home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / emacs / emacs18.57 / 1857sr~1.zoo / source / sysdep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  90.6 KB  |  4,053 lines

  1. /* Interfaces to system-dependent kernel and library entries.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /**
  21.  **  (sjk)++ include basepage stuff for dumping.
  22.  **/
  23. #if defined(atarist)
  24. #include <basepage.h>
  25. #include <ioctl.h>
  26. #endif 
  27.  
  28.  
  29. #include <signal.h>
  30. #include <setjmp.h>
  31.  
  32. #include "config.h"
  33. #include "lisp.h"
  34. #undef NULL
  35.  
  36. #define min(x,y) ((x) > (y) ? (y) : (x))
  37.  
  38. /* In this file, open, read and write refer to the system calls,
  39.    not our sugared interfaces  sys_open, sys_read and sys_write.
  40.    Contrariwise, for systems where we use the system calls directly,
  41.    define sys_read, etc. here as aliases for them.  */
  42. #ifndef read
  43. #define sys_read read
  44. #define sys_write write
  45. #endif /* `read' is not a macro */
  46.  
  47. #undef read
  48. #undef write
  49.  
  50. #ifndef close
  51. #define sys_close close
  52. #else 
  53. #undef close
  54. #endif
  55.  
  56. #ifndef open
  57. #define sys_open open
  58. #else /* `open' is a macro */
  59. #undef open
  60. #endif /* `open' is a macro */
  61.  
  62. #include <stdio.h>
  63. #include <sys/types.h>
  64. #include <sys/stat.h>
  65. #include <errno.h>
  66.  
  67. extern int errno;
  68. #ifndef VMS
  69. extern char *sys_errlist[];
  70. #endif
  71.  
  72. #ifdef VMS
  73. #include <rms.h>
  74. #include <ttdef.h>
  75. #include <tt2def.h>
  76. #include <iodef.h>
  77. #include <ssdef.h>
  78. #include <descrip.h>
  79. #include <ctype.h>
  80. #include <file.h>
  81. #ifndef RAB$C_BID
  82. #include <rab.h>
  83. #endif
  84. #define    MAXIOSIZE ( 32 * PAGESIZE )    /* Don't I/O more than 32 blocks at a time */
  85. #endif /* VMS */
  86.  
  87. #ifndef BSD4_1
  88. #ifdef BSD /* this is done this way to avoid defined(BSD) || defined (USG)
  89.           because the vms compiler doesn't grok `defined' */
  90. #include <fcntl.h>
  91. #endif
  92. #ifdef USG
  93. #include <fcntl.h>
  94. #endif
  95.  
  96. /**
  97.  **  (sjk)++ we need the flags...
  98.  **/
  99. #if defined(atarist) 
  100. #include <fcntl.h>
  101. #endif 
  102.  
  103. #endif /* not 4.1 bsd */
  104.  
  105. #ifdef BSD
  106. #include <sys/ioctl.h>
  107. #ifdef BSD4_1
  108. #include <wait.h>
  109. #else /* not 4.1 */
  110. #include <sys/wait.h>
  111. #endif /* not 4.1 */
  112. #endif /* BSD */
  113.  
  114. #ifdef STRIDE
  115. #include <sys/ioctl.h>
  116. #endif 
  117.  
  118. #ifdef mips
  119. #include <sys/ioctl.h>
  120. #endif 
  121.  
  122. #ifdef AIX
  123. /* Get files for keyboard remapping */
  124. #define HFNKEYS 2
  125. #include <sys/hft.h>
  126. #include <sys/devinfo.h>
  127. #endif
  128.  
  129. /* Get rid of LLITOUT in 4.1, since it is said to stimulate kernel bugs.  */
  130. #ifdef BSD4_1
  131. #undef LLITOUT
  132. #define LLITOUT 0
  133. #endif /* 4.1 */
  134.  
  135. #ifdef HAVE_TERMIO
  136. #ifdef IBMR2AIX
  137. #include <termio.h>
  138. #include <termios.h>
  139. #ifdef TIOCGETP
  140. #undef TIOCGETP
  141. #endif
  142. #define TIOCGETP TCGETS
  143. #undef TIOCSETN
  144. #define TIOCSETN TCSETSW
  145. #undef TIOCSETP
  146. #define TIOCSETP TCSETSF
  147. #undef TCSETAW
  148. #define TCSETAW TCSETS
  149. #else
  150. #include <termio.h>
  151. #undef TIOCGETP
  152. #define TIOCGETP TCGETA
  153. #undef TIOCSETN
  154. #define TIOCSETN TCSETAW
  155. #undef TIOCSETP
  156. #define TIOCSETP TCSETAF
  157. #endif
  158.  
  159. #ifndef IBMR2AIX
  160. #define TERMINAL struct termio
  161. #else
  162. #define TERMINAL struct termios
  163. #endif
  164.  
  165. #define OSPEED(str) (str.c_cflag & CBAUD)
  166. #define SETOSPEED(str,new) (str.c_cflag = (str.c_cflag & ~CBAUD) | (new))
  167. #define TABS_OK(str) ((str.c_oflag & TABDLY) != TAB3)
  168. #endif /* HAVE_TERMIO */
  169.  
  170. #ifdef XENIX
  171. #undef TIOCGETC  /* Avoid confusing some conditionals that test this.  */
  172. #endif
  173.  
  174. #ifdef BROKEN_TIOCGETC
  175. #undef TIOCGETC
  176. #endif
  177.  
  178. #ifndef HAVE_TERMIO
  179. #ifndef VMS
  180. #include <sgtty.h>
  181. #define TERMINAL struct sgttyb
  182. #define OSPEED(str) str.sg_ospeed
  183. #define SETOSPEED(str,new) (str.sg_ospeed = (new))
  184. #define TABS_OK(str) ((str.sg_flags & XTABS) != XTABS)
  185. #undef TCSETAW
  186. #define TCSETAW TIOCSETN
  187. #endif /* not VMS */
  188. #endif /* not HAVE_TERMIO */
  189.  
  190. #ifdef USG
  191. #include <sys/utsname.h>
  192. #include <memory.h>
  193. #include <string.h>
  194. #ifdef TIOCGWINSZ
  195. #ifndef IRIS_4D
  196. #ifndef mips
  197. #ifndef aix386
  198. #ifndef NO_SIOCTL_H
  199. /* Some USG systems with TIOCGWINSZ need this file; some don't have it.
  200.    We don't know how to distinguish them.
  201.    If this #include gets an error, just delete it.  */
  202. #include <sys/sioctl.h>
  203. #endif
  204. #ifdef NEED_PTEM_H
  205. #include <sys/stream.h>
  206. #include <sys/ptem.h>
  207. #endif
  208. #endif /* not aix386 */
  209. #endif /* not mips */
  210. #endif /* not IRIS_4D */
  211. #endif /* TIOCGWINSZ */
  212. #ifdef HAVE_TIMEVAL
  213. #ifdef HPUX
  214. #include <time.h>
  215. #else
  216. #include <sys/time.h>
  217. #endif
  218. #endif /* HAVE_TIMEVAL */
  219. #endif /* USG */
  220.  
  221. #ifdef VMS
  222. #include "window.h"
  223. #endif
  224.   
  225. #ifdef NEED_BSDTTY
  226. #include <sys/bsdtty.h>
  227. #endif 
  228.  
  229. #if defined (HPUX) && defined (HAVE_PTYS)
  230. #include <sys/ptyio.h>
  231. #endif
  232.   
  233. #ifdef AIX
  234. #include <sys/pty.h>
  235. #include <unistd.h>
  236. #endif /* AIX */
  237.  
  238. #ifdef SYSV_PTYS
  239. #include <sys/tty.h>
  240. #include <sys/pty.h>
  241. #endif
  242.  
  243. #ifdef BROKEN_FIONREAD
  244. #undef FIONREAD
  245. #endif
  246.  
  247. extern int quit_char;
  248.  
  249. #include "termhooks.h"
  250. #include "termchar.h"
  251. #include "termopts.h"
  252. #include "dispextern.h"
  253.  
  254. #ifdef NONSYSTEM_DIR_LIBRARY
  255. #include "ndir.h"
  256. #endif /* NONSYSTEM_DIR_LIBRARY */
  257.  
  258. #ifndef sigmask
  259. #define sigmask(no) (1L << ((no) - 1))
  260. #endif
  261.  
  262. /* Define SIGCHLD as an alias for SIGCLD.  There are many conditionals
  263.    testing SIGCHLD.  */
  264.  
  265. #ifndef VMS
  266. #ifdef SIGCLD
  267. #ifndef SIGCHLD
  268. #define SIGCHLD SIGCLD
  269. #endif /* not SIGCHLD */
  270. #endif /* SIGCLD */
  271. #endif /* not VMS */
  272.  
  273. static int baud_convert[] =
  274. #ifdef BAUD_CONVERT
  275.   BAUD_CONVERT;
  276. #else
  277.   {
  278.     0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
  279.     1800, 2400, 4800, 9600, 19200, 38400
  280.   };
  281. #endif
  282.  
  283. extern short ospeed;
  284.  
  285. #ifdef VMS
  286. static struct iosb
  287. {
  288.   short status;
  289.   short offset;
  290.   short termlen;
  291.   short term;
  292. } input_iosb;
  293.  
  294. int kbd_input_ast ();
  295.  
  296. int waiting_for_ast;
  297. int stop_input;
  298. int input_ef = 0;
  299. int timer_ef = 0;
  300. int process_ef = 0;
  301. int input_eflist;
  302. int timer_eflist;
  303.  
  304. static int input_chan;
  305. static $DESCRIPTOR (input_dsc, "TT");
  306. static int terminator_mask[2] = { 0, 0 };
  307.  
  308. static struct sensemode {
  309.   short status;
  310.   unsigned char xmit_baud;
  311.   unsigned char rcv_baud;
  312.   unsigned char crfill;
  313.   unsigned char lffill;
  314.   unsigned char parity;
  315.   unsigned char unused;
  316.   char class;
  317.   char type;
  318.   short scr_wid;
  319.   unsigned long tt_char : 24, scr_len : 8;
  320.   unsigned long tt2_char;
  321. } sensemode_iosb;
  322. #define TERMINAL struct sensemode
  323. #define OSPEED(str) (str.xmit_baud)
  324. #define TABS_OK(str) ((str.tt_char & TT$M_MECHTAB) != 0)
  325. #endif /* VMS */
  326.  
  327. discard_tty_input ()
  328. {
  329.   TERMINAL buf;
  330.  
  331.   if (noninteractive)
  332.     return;
  333.  
  334. #ifdef VMS
  335.   end_kbd_input ();
  336.   SYS$QIOW (0, input_chan, IO$_READVBLK|IO$M_PURGE, input_iosb, 0, 0,
  337.         &buf, 0, 0, terminator_mask, 0, 0);
  338.   queue_kbd_input ();
  339. #else /* not VMS */
  340.   ioctl (0, TIOCGETP, &buf);
  341.   ioctl (0, TIOCSETP, &buf);
  342. #endif /* not VMS */
  343. }
  344.  
  345. #ifdef SIGTSTP
  346.  
  347. stuff_char (c)
  348.      char c;
  349. {
  350. /* Should perhaps error if in batch mode */
  351. #ifdef TIOCSTI
  352.   ioctl (0, TIOCSTI, &c);
  353. #else /* no TIOCSTI */
  354.   error ("Cannot stuff terminal input characters in this version of Unix.");
  355. #endif /* no TIOCSTI */
  356. }
  357.  
  358. #endif /* SIGTSTP */
  359.  
  360. init_baud_rate ()
  361. {
  362.   TERMINAL sg;
  363.  
  364.   if (noninteractive)
  365.     ospeed = 0;
  366.   else
  367.     {
  368. #ifdef VMS
  369.       SYS$QIOW (0, input_chan, IO$_SENSEMODE, &sg, 0, 0,
  370.         &sg.class, 12, 0, 0, 0, 0 );
  371. #else
  372.       SETOSPEED (sg, B9600);
  373.       ioctl (0, TIOCGETP, &sg);
  374. #endif /* not VMS */
  375.       ospeed = OSPEED (sg);
  376.     }
  377.   
  378.   baud_rate = (ospeed < sizeof baud_convert / sizeof baud_convert[0]
  379.            ? baud_convert[ospeed] : 9600);
  380.   if (baud_rate == 0)
  381.     baud_rate = 1200;
  382. }
  383.  
  384. /*ARGSUSED*/
  385. set_exclusive_use (fd)
  386.      int fd;
  387. {
  388. #ifdef FIOCLEX
  389.   ioctl (fd, FIOCLEX, 0);
  390. #endif
  391.   /* Ok to do nothing if this feature does not exist */
  392. }
  393.  
  394. #ifndef subprocesses
  395.  
  396. wait_without_blocking ()
  397. {
  398. #ifdef BSD
  399.   wait3 (0, WNOHANG | WUNTRACED, 0);
  400. #else
  401.   croak ("wait_without_blocking");
  402. #endif
  403. }
  404.  
  405. #endif /* not subprocesses */
  406.  
  407. int wait_debugging;   /* Set nonzero to make following function work under dbx
  408.                  (at least for bsd).  */
  409.  
  410. /* Wait for subprocess with process id `pid' to terminate and
  411.    make sure it will get eliminated (not remain forever as a zombie) */
  412.  
  413. wait_for_termination (pid)
  414.      int pid;
  415. {
  416.   while (1)
  417.     {
  418. #ifdef subprocesses
  419. #if defined(BSD) || (defined(HPUX) && !defined(HPUX_5))
  420.       /* Note that kill returns -1 even if the process is just a zombie now.
  421.      But inevitably a SIGCHLD interrupt should be generated
  422.      and child_sig will do wait3 and make the process go away. */
  423.       /* There is some indication that there is a bug involved with
  424.      termination of subprocesses, perhaps involving a kernel bug too,
  425.      but no idea what it is.  Just as a hunch we signal SIGCHLD to see
  426.      if that causes the problem to go away or get worse.  */
  427. #ifdef BSD4_1
  428.       extern int synch_process_pid;
  429.       sighold (SIGCHLD);
  430.       if (synch_process_pid == 0)
  431.     {
  432.           sigrelse (SIGCHLD);
  433.       break;
  434.     }
  435.       if (wait_debugging)
  436.     sleep (1);
  437.       else
  438.     sigpause (SIGCHLD);
  439. #else /* not BSD4_1 */
  440.       sigsetmask (1 << (SIGCHLD - 1));
  441.       if (0 > kill (pid, 0))
  442.         {
  443.       sigsetmask (0);
  444.       kill (getpid (), SIGCHLD);
  445.       break;
  446.     }
  447.       if (wait_debugging)
  448.     sleep (1);
  449.       else
  450.     sigpause (0);
  451. #endif /* not BSD4_1 */
  452. #else /* not BSD, and not HPUX version >= 6 */
  453. #ifdef UNIPLUS
  454.       if (0 > kill (pid, 0))
  455.     break;
  456.       wait (0);
  457. #else /* neither BSD nor UNIPLUS: random sysV */
  458.       if (0 > kill (pid, 0))
  459.     break;
  460.       pause ();
  461. #endif /* not UNIPLUS */
  462. #endif /* not BSD, and not HPUX version >= 6 */
  463. #else /* not subprocesses */
  464. #ifndef BSD4_1
  465.  
  466. /**
  467.  **  (sjk)++ avoid all of the complicated UNIX hooplaha, and just quite!
  468.  **/
  469. #if defined(atarist)
  470.       break;
  471. #else
  472.       if (0 > kill (pid, 0))
  473.     break;
  474.       wait (0);
  475. #endif  /*  atarist  */
  476. #else /* BSD4_1 */
  477.       int status;
  478.       status = wait (0);
  479.       if (status == pid || status == -1)
  480.     break;
  481. #endif /* BSD4_1 */
  482. #endif /* not subprocesses */
  483.     }
  484. }
  485.  
  486. #ifdef subprocesses
  487.  
  488. /*
  489.  *    flush any pending output
  490.  *      (may flush input as well; it does not matter the way we use it)
  491.  */
  492.  
  493. flush_pending_output (channel)
  494.      int channel;
  495. {
  496. #ifdef TCFLSH
  497.   ioctl (channel, TCFLSH, 1);
  498. #else
  499. #ifdef TIOCFLUSH
  500.   int zero = 0;
  501.   /* 3rd arg should be ignored
  502.      but some 4.2 kernels actually want the address of an int
  503.      and nonzero means something different.  */
  504.   ioctl (channel, TIOCFLUSH, &zero);
  505. #endif
  506. #endif
  507. }
  508.  
  509. /*  Set up the terminal at the other end of a pseudo-terminal that
  510.     we will be controlling an inferior through.
  511.     It should not echo or do line-editing, since that is done
  512.     in Emacs.  No padding needed for insertion into an Emacs buffer.  */
  513.  
  514. child_setup_tty (out)
  515.      int out;
  516. {
  517.   TERMINAL s;
  518.  
  519.   ioctl (out, TIOCGETP, &s);
  520. #ifdef HAVE_TERMIO
  521.   s.c_oflag |= OPOST;        /* Enable output postprocessing */
  522.   s.c_oflag &= ~ONLCR;        /* Disable map of NL to CR-NL on output */
  523.   s.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);    /* No output delays */
  524.   s.c_lflag &= ~ECHO;        /* Disable echo */
  525.   s.c_lflag |= ISIG;        /* Enable signals */
  526.   s.c_iflag &= ~IUCLC;        /* Disable map of upper case to lower on input */
  527.   s.c_oflag &= ~OLCUC;        /* Disable map of lower case to upper on output */
  528. /* said to be unnecesary
  529.   s.c_cc[VMIN] = 1;        /* minimum number of characters to accept
  530.   s.c_cc[VTIME] = 0;        /* wait forever for at least 1 character
  531. */
  532.   s.c_lflag |= ICANON;        /* Enable erase/kill and eof processing */
  533.   s.c_cc[VEOF] = 04;        /* insure that EOF is Control-D */
  534.   s.c_cc[VERASE] = 0377;    /* disable erase processing */
  535.   s.c_cc[VKILL] = 0377;        /* disable kill processing */
  536. #ifdef HPUX
  537.   s.c_cflag = (s.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
  538. #endif HPUX
  539.  
  540. #ifdef AIX
  541. /* AIX enhanced edit loses NULs, so disable it */
  542. #ifndef IBMR2AIX
  543.   s.c_line = 0;
  544.   s.c_iflag &= ~ASCEDIT;
  545. #endif
  546.   /* Also, PTY overloads NUL and BREAK.
  547.      don't ignore break, but don't signal either, so it looks like NUL.  */
  548.   s.c_iflag &= ~IGNBRK;
  549.   s.c_iflag &= ~BRKINT;
  550. /* QUIT and INTR work better as signals, so disable character forms */
  551.   s.c_cc[VQUIT] = 0377;
  552.   s.c_cc[VINTR] = 0377;
  553.   s.c_cc[VEOL] = 0377;
  554.   s.c_lflag &= ~ISIG;
  555.   s.c_cflag = (s.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
  556. #endif /* AIX */
  557.  
  558. #else /* not HAVE_TERMIO */
  559.   s.sg_flags &= ~(ECHO | CRMOD | ANYP | ALLDELAY | RAW | LCASE | CBREAK | TANDEM);
  560. #endif /* not HAVE_TERMIO */
  561.  
  562.   ioctl (out, TIOCSETN, &s);
  563.  
  564. #ifdef BSD4_1
  565.   if (interrupt_input)
  566.     reset_sigio ();
  567. #endif /* BSD4_1 */
  568. #ifdef RTU
  569.   {
  570.     int zero = 0;
  571.     ioctl (out, FIOASYNC, &zero);
  572.   }
  573. #endif /* RTU */
  574. }
  575.  
  576. #endif /* subprocesses */
  577.  
  578. /*ARGSUSED*/
  579. setpgrp_of_tty (pid)
  580.      int pid;
  581. {
  582. #ifdef IBMR2AIX
  583.   tcsetpgrp ( 0, pid);
  584. #else
  585. #ifdef TIOCSPGRP
  586.   ioctl (0, TIOCSPGRP, &pid);
  587. #else
  588.   /* Just ignore this for now and hope for the best */
  589. #endif
  590. #endif
  591. }
  592.  
  593. /* Record a signal code and the handler for it.  */
  594. struct save_signal
  595. {
  596.   int code;
  597.   int (*handler) ();
  598. };
  599.  
  600. /* Suspend the Emacs process; give terminal to its superior.  */
  601. /**
  602.  **  (sjk)++
  603.  **/
  604. #if defined(atarist)
  605. #undef SIGTSTP
  606. #endif 
  607.  
  608. sys_suspend ()
  609. {
  610. /**
  611.  **  (sjk)++ to run a subshell, just system($SHELL) and hope the
  612.  **          system has enough memory allocated o make it usefull.
  613.  **/
  614. #if !defined(atarist)
  615.  
  616. #ifdef VMS
  617.   unsigned long parent_id;
  618.  
  619.   parent_id = getppid ();
  620.   if (parent_id && parent_id != 0xffffffff)
  621.     {
  622.       int oldsig = signal (SIGINT, SIG_IGN);
  623.       int status = LIB$ATTACH (&parent_id) & 1;
  624.       signal (SIGINT, oldsig);
  625.       return status;
  626.     }
  627.   return -1;
  628. #else
  629. #ifdef SIGTSTP
  630. #ifdef BSD
  631.   killpg (getpgrp (0), SIGTSTP);
  632. #else
  633.   kill (-getpgrp (0), SIGTSTP);
  634. #endif
  635.  
  636. #else
  637. #ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
  638.   ptrace (0, 0, 0, 0);        /* set for ptrace - caught by csh */
  639.   kill (getpid (), SIGQUIT);
  640.  
  641. #else
  642.  
  643. /* On a system where suspending is not implemented,
  644.    instead fork a subshell and let it talk directly to the terminal
  645.    while we wait.  */
  646.  
  647.   int pid = fork ();
  648.  
  649.   struct save_signal saved_handlers[5];
  650.  
  651.   saved_handlers[0].code = SIGINT;
  652.   saved_handlers[1].code = SIGQUIT;
  653.   saved_handlers[2].code = SIGTERM;
  654. #ifdef SIGIO
  655.   saved_handlers[3].code = SIGIO;
  656.   saved_handlers[4].code = 0;
  657. #else
  658.   saved_handlers[3].code = 0;
  659. #endif
  660.  
  661.   if (pid == -1)
  662.     error ("Can't spawn subshell");
  663.   if (pid == 0)
  664.     {
  665.       char *sh;
  666.       sh = (char *) egetenv ("SHELL");
  667.  
  668.       if (sh == 0)
  669.     sh = "sh";
  670.  
  671.       /* Use our buffer's default directory for the subshell.  */
  672.       {
  673.     Lisp_Object dir;
  674.     unsigned char *str;
  675.     int len;
  676.  
  677.     /* mentioning current_buffer->buffer would mean including buffer.h,
  678.        which somehow wedges the hp compiler.  So instead... */
  679.  
  680.     dir = intern ("default-directory");
  681.     /* Can't use NULL */
  682.     if (XFASTINT (Fboundp (dir)) == XFASTINT (Qnil))
  683.       goto xyzzy;
  684.     dir = Fsymbol_value (dir);
  685.     if (XTYPE (dir) != Lisp_String)
  686.       goto xyzzy;
  687.  
  688.     str = (unsigned char *) alloca (XSTRING (dir)->size + 2);
  689.     len = XSTRING (dir)->size;
  690.     bcopy (XSTRING (dir)->data, str, len);
  691.     if (str[len - 1] != '/') str[len++] = '/';
  692.     str[len] = 0;
  693.     chdir (str);
  694.       }
  695.     xyzzy:
  696. #ifdef subprocesses
  697.       close_process_descs ();    /* Close Emacs's pipes/ptys */
  698. #endif
  699.       execlp (sh, sh, 0);
  700.       write (1, "Can't execute subshell", 22);
  701.       _exit (1);
  702.     }
  703.   save_signal_handlers (&saved_handlers);
  704.   wait_for_termination (pid);
  705.   restore_signal_handlers (&saved_handlers);
  706. #endif /* no USG_JOBCTRL */
  707. #endif /* no SIGTSTP */
  708. #endif /* not VMS */
  709. /**
  710.  **  (sjk)++ Launch a asyncronous shell.
  711.  **/
  712. #else /* atarist */
  713.   { char *shell; 
  714.     shell = getenv("SHELL");
  715.     if (shell!=0) system(shell);
  716.       else 
  717.         fprintf(stderr,"Set env-var SHELL.\n");
  718.   }
  719. #endif /*  atarist  */
  720.  
  721. }
  722.  
  723. save_signal_handlers (saved_handlers)
  724.      struct save_signal *saved_handlers;
  725. {
  726.   while (saved_handlers->code)
  727.     {
  728.       saved_handlers->handler
  729.     = (int (*) ()) signal (saved_handlers->code, SIG_IGN);
  730.       saved_handlers++;
  731.     }
  732. }
  733.  
  734. restore_signal_handlers (saved_handlers)
  735.      struct save_signal *saved_handlers;
  736. {
  737.   while (saved_handlers->code)
  738.     {
  739. /**
  740.  **  (sjk)++ a simple typecast
  741.  **/
  742. #if defined(atarist)
  743.       signal (saved_handlers->code, (__Sigfunc)saved_handlers->handler);
  744. #else
  745.       signal (saved_handlers->code, saved_handlers->handler);
  746. #endif
  747.       saved_handlers++;
  748.     }
  749. }
  750.  
  751. /**
  752.  **  (sjk)++ a SIGIO will cause a system croak().
  753.  **/
  754. #if !defined(atarist)
  755.  
  756. #ifdef F_SETFL
  757.  
  758. int old_fcntl_flags;
  759.  
  760. init_sigio ()
  761. {
  762. #ifdef FASYNC
  763.   old_fcntl_flags = fcntl (0, F_GETFL, 0) & ~FASYNC;
  764. #endif
  765.   request_sigio ();
  766. }
  767.  
  768. reset_sigio ()
  769. {
  770.   unrequest_sigio ();
  771. }
  772.  
  773. #ifdef FASYNC        /* F_SETFL does not imply existance of FASYNC */
  774.  
  775. request_sigio ()
  776. {
  777. #ifdef SIGWINCH
  778.   int omask = sigblock (0);
  779.   sigsetmask (omask & ~sigmask (SIGWINCH));
  780. #endif
  781.   fcntl (0, F_SETFL, old_fcntl_flags | FASYNC);
  782.  
  783.   interrupts_deferred = 0;
  784. }
  785.  
  786. unrequest_sigio ()
  787. {
  788. #ifdef SIGWINCH
  789.   sigblock (sigmask (SIGWINCH));
  790. #endif
  791.   fcntl (0, F_SETFL, old_fcntl_flags);
  792.   interrupts_deferred = 1;
  793. }
  794.  
  795. #else /* no FASYNC */
  796. #ifdef STRIDE        /* Stride doesn't have FASYNC - use FIOASYNC */
  797.  
  798. request_sigio ()
  799. {
  800.   int on = 1;
  801.   ioctl (0, FIOASYNC, &on);
  802.   interrupts_deferred = 0;
  803. }
  804.  
  805. unrequest_sigio ()
  806. {
  807.   int off = 0;
  808.  
  809.   ioctl (0, FIOASYNC, &off);
  810.   interrupts_deferred = 1;
  811. }
  812.  
  813. #else /* not FASYNC, not STRIDE */
  814.  
  815. request_sigio ()
  816. {
  817.   croak ("request_sigio");
  818. }
  819.  
  820. unrequest_sigio ()
  821. {
  822.   croak ("unrequest_sigio");
  823. }
  824.  
  825. #endif /* STRIDE */
  826. #endif /* FASYNC */
  827. #endif /* F_SETFL */
  828. #endif /* !atarist */
  829. /**
  830.  **  (sjk)++ see previous ST comment.
  831.  **/
  832. #ifdef atarist
  833.  
  834. unrequest_sigio ()
  835. {
  836.   croak ("unrequest_sigio");
  837. }
  838.  
  839. croak (badfunc)
  840.      char *badfunc;
  841. {
  842.   printf ("%s not yet implemented\r\n", badfunc);
  843. }
  844. #endif  /*  atarist  */
  845.  
  846.  
  847. TERMINAL old_gtty;        /* The initial tty mode bits */
  848.  
  849. int term_initted;        /* 1 if outer tty status has been recorded */
  850.  
  851. #ifdef F_SETOWN
  852. int old_fcntl_owner;
  853. #endif /* F_SETOWN */
  854.  
  855. #ifdef TIOCGLTC
  856. struct ltchars old_ltchars;
  857. #endif /* TIOCGLTC */
  858.  
  859. #ifdef TIOCGETC
  860. struct tchars old_tchars;
  861. int old_lmode;
  862.  
  863. int lmode;            /* Current lmode value. */
  864.                 /* Needed as global for 4.1 */
  865. #endif /* TIOCGETC */
  866.  
  867. /* This may also be defined in stdio,
  868.    but if so, this does no harm,
  869.    and using the same name avoids wasting the other one's space.  */
  870.  
  871. #ifdef USG
  872. unsigned char _sobuf[BUFSIZ+8];
  873. #else
  874. char _sobuf[BUFSIZ];
  875. #endif
  876.  
  877. #ifdef TIOCGLTC
  878. static struct ltchars new_ltchars = {-1,-1,-1,-1,-1,-1};
  879. #endif
  880. #ifdef TIOCGETC
  881.   static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1};
  882. #endif 
  883.  
  884. init_sys_modes ()
  885. {
  886.   TERMINAL tty;
  887. #ifdef TIOCGETC
  888.   struct tchars tchars;
  889. #endif
  890. #ifdef VMS
  891. #if 0
  892.   static int oob_chars[2] = {0, 1 << 7}; /* catch C-g's */
  893.   extern int (*interrupt_signal) ();
  894. #endif
  895. #endif
  896.  
  897.   if (noninteractive)
  898.     return;
  899.  
  900. #ifdef VMS
  901.   if (!input_ef)
  902.     LIB$GET_EF (&input_ef);
  903.   SYS$CLREF (input_ef);
  904.   waiting_for_ast = 0;
  905.   if (!timer_ef)
  906.     LIB$GET_EF (&timer_ef);
  907.   SYS$CLREF (timer_ef);
  908.   if (!process_ef)
  909.     {
  910.       LIB$GET_EF (&process_ef);
  911.       SYS$CLREF (process_ef);
  912.     }
  913.   if (input_ef / 32 != process_ef / 32)
  914.     croak ("Input and process event flags in different clusters.");
  915.   if (input_ef / 32 != timer_ef / 32)
  916.     croak ("Input and process event flags in different clusters.");
  917.   input_eflist = ((unsigned) 1 << (input_ef % 32)) |
  918.     ((unsigned) 1 << (process_ef % 32));
  919.   timer_eflist = ((unsigned) 1 << (input_ef % 32)) |
  920.     ((unsigned) 1 << (timer_ef % 32));
  921.   SYS$QIOW (0, input_chan, IO$_SENSEMODE, &old_gtty, 0, 0,
  922.         &old_gtty.class, 12, 0, 0, 0, 0);
  923. #ifndef VMS4_4
  924.   sys_access_reinit ();
  925. #endif
  926. #else /* not VMS */
  927.   ioctl (0, TIOCGETP, &old_gtty);
  928. #endif /* not VMS */
  929.   if (!read_socket_hook)
  930.     {
  931.       tty = old_gtty;
  932.  
  933. #ifdef HAVE_TERMIO
  934.       tty.c_iflag |= (IGNBRK);    /* Ignore break condition */
  935.       tty.c_iflag &= ~ICRNL;    /* Disable map of CR to NL on input */
  936. #ifdef ISTRIP
  937.       tty.c_iflag &= ~ISTRIP;    /* don't strip 8th bit on input */
  938. #endif
  939.       tty.c_lflag &= ~ECHO;    /* Disable echo */
  940.       tty.c_lflag &= ~ICANON;    /* Disable erase/kill processing */
  941.       tty.c_lflag |= ISIG;    /* Enable signals */
  942.       if (flow_control)
  943.     {
  944.       tty.c_iflag |= IXON;    /* Enable start/stop output control */
  945. #ifdef IXANY
  946.       tty.c_iflag &= ~IXANY;
  947. #endif /* IXANY */
  948.     }
  949.       else
  950.     tty.c_iflag &= ~IXON;    /* Disable start/stop output control */
  951.       tty.c_oflag &= ~ONLCR;    /* Disable map of NL to CR-NL on output */
  952.       tty.c_oflag &= ~TAB3;    /* Disable tab expansion */
  953. #ifdef CS8
  954.       tty.c_cflag |= CS8;    /* allow 8th bit on input */
  955.       tty.c_cflag &= ~PARENB;    /* Don't check parity */
  956. #endif
  957.       tty.c_cc[VINTR] = quit_char;    /* ^G gives SIGINT */
  958.       /* Set up C-g (usually) for both SIGQUIT and SIGINT.
  959.      We don't know which we will get, but we handle both alike
  960.      so which one it really gives us does not matter.  */
  961.       tty.c_cc[VQUIT] = quit_char;
  962.       tty.c_cc[VMIN] = 1;    /* Input should wait for at least 1 char */
  963.       tty.c_cc[VTIME] = 0;    /* no matter how long that takes.  */
  964. #ifdef VSWTCH
  965.       tty.c_cc[VSWTCH] = CDEL;    /* Turn off shell layering use of C-z */
  966. #endif /* VSWTCH */
  967. #ifdef mips  /* The following code looks like the right thing in general,
  968.         but it is said to cause a crash on USG V.4.
  969.         Let's play safe by turning it on only for the MIPS.  */
  970. #ifdef VSUSP
  971.       tty.c_cc[VSUSP] = CDEL;    /* Turn off mips handling of C-z.  */
  972. #endif /* VSUSP */
  973. #ifdef V_DSUSP
  974.       tty.c_cc[V_DSUSP] = CDEL;    /* Turn off mips handling of C-y.  */
  975. #endif /* V_DSUSP */
  976. #endif /* mips */
  977.  
  978. #ifdef AIX
  979. #ifndef IBMR2AIX
  980.       /* AIX enhanced edit loses NULs, so disable it */
  981.       tty.c_line = 0;
  982.       tty.c_iflag &= ~ASCEDIT;
  983. #else
  984.       tty.c_cc[VSTRT] = 255;
  985.       tty.c_cc[VSTOP] = 255;
  986.       tty.c_cc[VSUSP] = 255;
  987.       tty.c_cc[VDSUSP] = 255;
  988. #endif /* IBMR2AIX */
  989.       /* Also, PTY overloads NUL and BREAK.
  990.      don't ignore break, but don't signal either, so it looks like NUL.
  991.      This really serves a purpose only if running in an XTERM window
  992.      or via TELNET or the like, but does no harm elsewhere.  */
  993.       tty.c_iflag &= ~IGNBRK;
  994.       tty.c_iflag &= ~BRKINT;
  995. #endif /* AIX */
  996.  
  997. #else /* if not HAVE_TERMIO */
  998. #ifdef VMS
  999.       tty.tt_char |= TT$M_NOECHO | TT$M_EIGHTBIT;
  1000.       if (flow_control)
  1001.     tty.tt_char |= TT$M_TTSYNC;
  1002.       else
  1003.     tty.tt_char &= ~TT$M_TTSYNC;
  1004.       tty.tt2_char |= TT2$M_PASTHRU | TT2$M_XON;
  1005. #else /* not VMS (BSD, that is) */
  1006.       tty.sg_flags &= ~(ECHO | CRMOD | XTABS);
  1007.       tty.sg_flags |= ANYP;
  1008.       tty.sg_flags |= interrupt_input ? RAW : CBREAK;
  1009. #endif /* not VMS (BSD, that is) */
  1010. #endif /* not HAVE_TERMIO */
  1011.  
  1012. #ifdef VMS
  1013.       SYS$QIOW (0, input_chan, IO$_SETMODE, &input_iosb, 0, 0,
  1014.         &tty.class, 12, 0, 0, 0, 0);
  1015. #else
  1016.       ioctl (0, TIOCSETN, &tty);
  1017. #endif /* not VMS */
  1018.  
  1019.       /* This code added to insure that, if flow-control is not to be used,
  1020.      we have an unlocked screen at the start. */
  1021. #ifdef TCXONC
  1022.       if (!flow_control) ioctl (0, TCXONC, 1);
  1023. #endif
  1024. #ifdef TIOCSTART
  1025.       if (!flow_control) ioctl (0, TIOCSTART, 0);
  1026. #endif
  1027.  
  1028. #ifdef AIX
  1029.       hft_init ();
  1030. #ifdef IBMR2AIX
  1031.   {
  1032.     /* IBM's HFT device usually thinks a ^J should be LF/CR.  We need it
  1033.        to be only LF.  This is the way that is done. */
  1034.     struct termio tty;
  1035.  
  1036.     if (ioctl (1, HFTGETID, &tty) != -1)
  1037.       write (1, "\033[20l", 5);
  1038.   }
  1039. #endif
  1040. #endif
  1041.  
  1042. #ifdef F_SETFL
  1043. #ifdef F_GETOWN        /* F_SETFL does not imply existance of F_GETOWN */
  1044.       if (interrupt_input)
  1045.     {
  1046.       old_fcntl_owner = fcntl (0, F_GETOWN, 0);
  1047.       fcntl (0, F_SETOWN, getpid ());
  1048.       init_sigio ();
  1049.     }
  1050. #endif /* F_GETOWN */
  1051. #endif /* F_SETFL */
  1052.  
  1053.       /* If going to use CBREAK mode, we must request C-g to interrupt
  1054.        and turn off start and stop chars, etc.
  1055.        If not going to use CBREAK mode, do this anyway
  1056.        so as to turn off local flow control for user coming over
  1057.        network on 4.2; in this case, only t_stopc and t_startc really matter.  */
  1058. #ifdef TIOCGLTC
  1059.       ioctl (0, TIOCGLTC, &old_ltchars);
  1060. #endif /* TIOCGLTC */
  1061. #ifndef HAVE_TERMIO
  1062. #ifdef TIOCGETC
  1063.       ioctl (0, TIOCGETC, &old_tchars);
  1064. /**
  1065.  **  (sjk)++ limit the amount of terminal init, since we use 
  1066.  **          console_read_byte almost exclusively.
  1067.  **/
  1068. #if !defined(atarist)
  1069.       ioctl (0, TIOCLGET, &old_lmode);
  1070. #endif
  1071.  
  1072.       /* Note: if not using CBREAK mode, it makes no difference how we set this */
  1073.       tchars = new_tchars;
  1074.       tchars.t_intrc = quit_char;
  1075.       if (flow_control)
  1076.     {
  1077.       tchars.t_startc = '\021';
  1078.       tchars.t_stopc = '\023';
  1079.     }
  1080. /* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits.  */
  1081. #ifndef LPASS8
  1082. #define LPASS8 0
  1083. #endif
  1084.  
  1085. #ifdef BSD4_1
  1086. #define LNOFLSH 0100000
  1087. #endif
  1088.  
  1089. /**
  1090.  **  (sjk)++ Force all 8 bits to go through on a read
  1091.  **/
  1092. #if !defined(atarist)
  1093.       lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH | old_lmode;
  1094. #endif
  1095.  
  1096.       ioctl (0, TIOCSETC, &tchars);
  1097.  
  1098. /**
  1099.  **  (sjk)++ We do not set the local chars on the ST
  1100.  **/
  1101. #if !defined(atarist)
  1102.       ioctl (0, TIOCLSET, &lmode);
  1103. #endif
  1104.  
  1105. #endif /* TIOCGETC */
  1106. #endif /* not HAVE_TERMIO */
  1107. #ifdef TIOCGLTC
  1108.       ioctl (0, TIOCSLTC, &new_ltchars);
  1109. #endif /* TIOCGLTC */
  1110.  
  1111. #ifdef BSD4_1
  1112.       if (interrupt_input)
  1113.     init_sigio ();
  1114. #endif
  1115. #ifdef VMS
  1116. /*  Appears to do nothing when in PASTHRU mode.
  1117.       SYS$QIOW (0, input_chan, IO$_SETMODE|IO$M_OUTBAND, 0, 0, 0,
  1118.         interrupt_signal, oob_chars, 0, 0, 0, 0);
  1119. */
  1120.       queue_kbd_input (0);
  1121. #endif /* VMS */
  1122.     }
  1123. #ifdef VMS  /* VMS sometimes has this symbol but lacks setvbuf.  */
  1124. #undef _IOFBF
  1125. #endif
  1126. #ifdef _IOFBF
  1127.   /* This symbol is defined on recent USG systems.
  1128.      Someone says without this call USG won't really buffer the file
  1129.      even with a call to setbuf(). */
  1130.   setvbuf (stdout, _sobuf, _IOFBF, sizeof _sobuf);
  1131. #else
  1132.   setbuf (stdout, _sobuf);
  1133. #endif
  1134.   set_terminal_modes ();
  1135.   if (term_initted && no_redraw_on_reenter)
  1136.     {
  1137.       if (display_completed)
  1138.     direct_output_forward_char (0);
  1139.     }
  1140.   else
  1141.     screen_garbaged = 1;
  1142.   term_initted = 1;
  1143. }
  1144.  
  1145. /* Return nonzero if safe to use tabs in output.
  1146.    At the time this is called, init_sys_modes has not been done yet.  */
  1147.    
  1148. tabs_safe_p ()
  1149. {
  1150.   TERMINAL tty;
  1151.   if (noninteractive)
  1152.     return 1;
  1153. #ifdef VMS
  1154.   SYS$QIOW (0, input_chan, IO$_SENSEMODE, &tty, 0, 0,
  1155.         &tty.class, 12, 0, 0, 0, 0);
  1156. #else
  1157.   ioctl (0, TIOCGETP, &tty);
  1158. #endif /* not VMS */
  1159.   return (TABS_OK(tty));
  1160. }
  1161.  
  1162. /* Get terminal size from system.
  1163.    Store number of lines into *heightp and width into *widthp.
  1164.    If zero or a negative number is stored, the value is not valid.  */
  1165.  
  1166. get_screen_size (widthp, heightp)
  1167.      int *widthp, *heightp;
  1168. {
  1169. /* Define the 4.3 names in terms of the Sun names
  1170.    if the latter exist and the former do not.  */
  1171. #ifdef TIOCGSIZE
  1172. #ifndef TIOCGWINSZ
  1173. #define TIOCGWINSZ TIOCGSIZE
  1174. #define winsize ttysize
  1175. #define ws_row ts_lines
  1176. #define ws_col ts_cols
  1177. #endif
  1178. #endif /* Sun */
  1179.  
  1180. /* Do it using the 4.3 names if possible.  */
  1181. #ifdef TIOCGWINSZ
  1182.   struct winsize size;
  1183.   *widthp = 0;
  1184.   *heightp = 0;
  1185.   if (ioctl (0, TIOCGWINSZ, &size) < 0)
  1186.     return;
  1187.   *widthp = size.ws_col;
  1188.   *heightp = size.ws_row;
  1189. #else /* not TIOCGWNSIZ */
  1190. #ifdef VMS
  1191.   TERMINAL tty;
  1192.   SYS$QIOW (0, input_chan, IO$_SENSEMODE, &tty, 0, 0,
  1193.         &tty.class, 12, 0, 0, 0, 0);
  1194.   *widthp = tty.scr_wid;
  1195.   *heightp = tty.scr_len;
  1196. #else /* system doesn't know size */
  1197.   *widthp = 0;
  1198.   *heightp = 0;
  1199. #endif /* system does not know size */
  1200. #endif /* not TIOCGWINSZ */
  1201. }
  1202.  
  1203. reset_sys_modes ()
  1204. {
  1205.   if (noninteractive)
  1206.     {
  1207.       fflush (stdout);
  1208.       return;
  1209.     }
  1210.   if (!term_initted)
  1211.     return;
  1212.   if (read_socket_hook)
  1213.     return;
  1214.   move_cursor (screen_height - 1, 0);
  1215.   clear_end_of_line (screen_width);
  1216.   /* clear_end_of_line may move the cursor */
  1217.   move_cursor (screen_height - 1, 0);
  1218.   /* Output raw CR so kernel can track the cursor hpos.  */
  1219.   cmputc ('\r');
  1220. #ifdef IBMR2AIX
  1221.   {
  1222.     /* HFT devices normally use ^J as a LF/CR.  We forced it to 
  1223.        do the LF only.  Now, we need to reset it. */
  1224.     struct termio tty;
  1225.  
  1226.     if (ioctl (1, HFTGETID, &tty) != -1)
  1227.       write (1, "\033[20h", 5);
  1228.   }
  1229. #endif
  1230.  
  1231.   reset_terminal_modes ();
  1232.   fflush (stdout);
  1233. #ifdef BSD
  1234. #ifndef BSD4_1
  1235.   /* Avoid possible loss of output when changing terminal modes.  */
  1236.   fsync (fileno (stdout));
  1237. #endif
  1238. #endif
  1239. #ifdef TIOCGLTC
  1240.   ioctl (0, TIOCSLTC, &old_ltchars);
  1241. #endif /* TIOCGLTC */
  1242. #ifndef HAVE_TERMIO
  1243. #ifdef TIOCGETC
  1244.   ioctl (0, TIOCSETC, &old_tchars);
  1245. /**
  1246.  **  (sjk)++ limit ioctl() calls (not needed)
  1247.  **/
  1248. #if !defined(atarist)
  1249.   ioctl (0, TIOCLSET, &old_lmode);
  1250. #endif
  1251.  
  1252. #endif /* TIOCGETC */
  1253. #endif /* not HAVE_TERMIO */
  1254. #ifdef F_SETFL
  1255. #ifdef F_SETOWN        /* F_SETFL does not imply existance of F_SETOWN */
  1256.   if (interrupt_input)
  1257.     {
  1258.       reset_sigio ();
  1259.       fcntl (0, F_SETOWN, old_fcntl_owner);
  1260.     }
  1261. #endif /* F_SETOWN */
  1262. #endif /* F_SETFL */
  1263. #ifdef BSD4_1
  1264.   if (interrupt_input)
  1265.     reset_sigio ();
  1266. #endif /* BSD4_1 */
  1267. #ifdef VMS
  1268.   end_kbd_input ();
  1269.   SYS$QIOW (0, input_chan, IO$_SETMODE, &input_iosb, 0, 0,
  1270.         &old_gtty.class, 12, 0, 0, 0, 0);
  1271. #else /* not VMS */
  1272.   while (ioctl (0, TCSETAW, &old_gtty) < 0 && errno == EINTR);
  1273. #endif /* not VMS */
  1274.  
  1275. #ifdef AIX
  1276.   hft_reset ();
  1277. #endif
  1278. }
  1279.  
  1280. #ifdef HAVE_PTYS
  1281.  
  1282. /* Set up the proper status flags for use of a pty.  */
  1283.  
  1284. setup_pty (fd)
  1285.      int fd;
  1286. {
  1287.   /* I'm told that TOICREMOTE does not mean control chars
  1288.      "can't be sent" but rather that they don't have
  1289.      input-editing or signaling effects.
  1290.      That should be good, because we have other ways
  1291.      to do those things in Emacs.
  1292.      However, telnet mode seems not to work on 4.2.
  1293.      So TIOCREMOTE is turned off now. */
  1294.  
  1295.   /* Under hp-ux, if TIOCREMOTE is turned on, some calls
  1296.      will hang.  In particular, the "timeout" feature (which
  1297.      causes a read to return if there is no data available)
  1298.      does this.  Also it is known that telnet mode will hang
  1299.      in such a way that Emacs must be stopped (perhaps this
  1300.      is the same problem).
  1301.      
  1302.      If TIOCREMOTE is turned off, then there is a bug in
  1303.      hp-ux which sometimes loses data.  Apparently the
  1304.      code which blocks the master process when the internal
  1305.      buffer fills up does not work.  Other than this,
  1306.      though, everything else seems to work fine.
  1307.      
  1308.      Since the latter lossage is more benign, we may as well
  1309.      lose that way.  -- cph */
  1310. #ifdef FIONBIO
  1311. #ifdef SYSV_PTYS
  1312.   {
  1313.     int on = 1;
  1314.     ioctl (fd, FIONBIO, &on);
  1315.   }
  1316. #endif
  1317. #endif
  1318. #ifdef IBMRTAIX
  1319.   /* On AIX, the parent gets SIGHUP when a pty attached child dies.  So, we */
  1320.   /* ignore SIGHUP once we've started a child on a pty.  Note that this may */
  1321.   /* cause EMACS not to die when it should, i.e., when its own controlling  */
  1322.   /* tty goes away.  I've complained to the AIX developers, and they may    */
  1323.   /* change this behavior, but I'm not going to hold my breath.             */
  1324.   signal (SIGHUP, SIG_IGN);
  1325. #endif
  1326. }
  1327. #endif /* HAVE_PTYS */
  1328.  
  1329. #ifdef VMS
  1330.  
  1331. /* Assigning an input channel is done at the start of Emacs execution.
  1332.    This is called each time Emacs is resumed, also, but does nothing
  1333.    because input_chain is no longer zero.  */
  1334.  
  1335. init_vms_input()
  1336. {
  1337.   int status;
  1338.   
  1339.   if (input_chan == 0)
  1340.     {
  1341.       status = SYS$ASSIGN (&input_dsc, &input_chan, 0, 0);
  1342.       if (! (status & 1))
  1343.     LIB$STOP (status);
  1344.     }
  1345. }
  1346.  
  1347. /* Deassigning the input channel is done before exiting.  */
  1348.  
  1349. stop_vms_input ()
  1350. {
  1351.   return SYS$DASSGN (input_chan);
  1352. }
  1353.  
  1354. short input_buffer;
  1355.  
  1356. /* Request reading one character into the keyboard buffer.
  1357.    This is done as soon as the buffer becomes empty.  */
  1358.  
  1359. queue_kbd_input ()
  1360. {
  1361.   int status;
  1362.   waiting_for_ast = 0;
  1363.   stop_input = 0;
  1364.   status = SYS$QIO (0, input_chan, IO$_READVBLK,
  1365.             &input_iosb, kbd_input_ast, 1,
  1366.             &input_buffer, 1, 0, terminator_mask, 0, 0);
  1367. }
  1368.  
  1369. int input_count;
  1370.  
  1371. /* Ast routine that is called when keyboard input comes in
  1372.    in accord with the SYS$QIO above.  */
  1373.  
  1374. kbd_input_ast ()
  1375. {
  1376.   register int c = -1;
  1377.   int old_errno = errno;
  1378.  
  1379.   if (waiting_for_ast)
  1380.     SYS$SETEF (input_ef);
  1381.   waiting_for_ast = 0;
  1382.   input_count++;
  1383. #ifdef ASTDEBUG
  1384.   if (input_count == 25)
  1385.     exit (1);
  1386.   printf ("Ast # %d,", input_count);
  1387.   printf (" iosb = %x, %x, %x, %x",
  1388.       input_iosb.offset, input_iosb.status, input_iosb.termlen,
  1389.       input_iosb.term);
  1390. #endif
  1391.   if (input_iosb.offset)
  1392.     {
  1393.       c = input_buffer;
  1394. #ifdef ASTDEBUG
  1395.       printf (", char = 0%o", c);
  1396. #endif
  1397.     }
  1398. #ifdef ASTDEBUG
  1399.   printf ("\n");
  1400.   fflush (stdout);
  1401.   sleep (1);
  1402. #endif
  1403.   if (! stop_input)
  1404.     queue_kbd_input ();
  1405.   if (c >= 0)
  1406.     kbd_buffer_store_char (c);
  1407.  
  1408.   errno = old_errno;
  1409. }
  1410.  
  1411. /* Wait until there is something in kbd_buffer.  */
  1412.  
  1413. wait_for_kbd_input ()
  1414. {
  1415.   extern int have_process_input, process_exited;
  1416.  
  1417.   /* If already something, avoid doing system calls.  */
  1418.   if (detect_input_pending ())
  1419.     {
  1420.       return;
  1421.     }
  1422.   /* Clear a flag, and tell ast routine above to set it.  */
  1423.   SYS$CLREF (input_ef);
  1424.   waiting_for_ast = 1;
  1425.   /* Check for timing error: ast happened while we were doing that.  */
  1426.   if (!detect_input_pending ())
  1427.     {
  1428.       /* No timing error: wait for flag to be set.  */
  1429.       set_waiting_for_input (0);
  1430.       SYS$WFLOR (input_ef, input_eflist);
  1431.       clear_waiting_for_input (0);
  1432.       if (!detect_input_pending ())
  1433.     /* Check for subprocess input availability */
  1434.     {
  1435.       int dsp = have_process_input || process_exited;
  1436.  
  1437.       sys$clref (process_ef);
  1438.       if (have_process_input)
  1439.         process_command_input ();
  1440.       if (process_exited)
  1441.         process_exit ();
  1442.       if (dsp)
  1443.         {
  1444.           update_mode_lines++;
  1445.           redisplay_preserve_echo_area ();
  1446.         }
  1447.     }
  1448.     }
  1449.   waiting_for_ast = 0;
  1450. }
  1451.  
  1452. /* Get rid of any pending QIO, when we are about to suspend
  1453.    or when we want to throw away pending input.
  1454.    We wait for a positive sign that the AST routine has run
  1455.    and therefore there is no I/O request queued when we return.
  1456.    SYS$SETAST is used to avoid a timing error.  */
  1457.  
  1458. end_kbd_input()
  1459. {
  1460. #ifdef ASTDEBUG
  1461.   printf ("At end_kbd_input.\n");
  1462.   fflush (stdout);
  1463.   sleep (1);
  1464. #endif
  1465.   if (LIB$AST_IN_PROG ())  /* Don't wait if suspending from kbd_buffer_store_char! */
  1466.     {
  1467.       SYS$CANCEL (input_chan);
  1468.       return;
  1469.     }
  1470.  
  1471.   SYS$SETAST (0);
  1472.   /* Clear a flag, and tell ast routine above to set it.  */
  1473.   SYS$CLREF (input_ef);
  1474.   waiting_for_ast = 1;
  1475.   stop_input = 1;
  1476.   SYS$CANCEL (input_chan);
  1477.   SYS$SETAST (1);
  1478.   SYS$WAITFR (input_ef);
  1479.   waiting_for_ast = 0;
  1480. }
  1481.  
  1482. /* Wait for either input available or time interval expiry.  */
  1483.  
  1484. input_wait_timeout (timeval)
  1485.      int timeval;        /* Time to wait, in seconds */
  1486. {
  1487.   int time [2];
  1488.   
  1489.   LIB$EMUL (&timeval, &-10000000, &0, time);       /* Convert to VMS format */
  1490.  
  1491.   /* If already something, avoid doing system calls.  */
  1492.   if (detect_input_pending ())
  1493.     {
  1494.       return;
  1495.     }
  1496.   /* Clear a flag, and tell ast routine above to set it.  */
  1497.   SYS$CLREF (input_ef);
  1498.   waiting_for_ast = 1;
  1499.   /* Check for timing error: ast happened while we were doing that.  */
  1500.   if (!detect_input_pending ())
  1501.     {
  1502.       /* No timing error: wait for flag to be set.  */
  1503.       SYS$CANTIM (1, 0);
  1504.       if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
  1505.     SYS$WFLOR (timer_ef, timer_eflist);      /* Wait for timer expiry or input */
  1506.     }
  1507.   waiting_for_ast = 0;
  1508. }
  1509.  
  1510. /* The standard `sleep' routine works some other way
  1511.    and it stops working if you have ever quit out of it.
  1512.    This one continues to work.  */
  1513.  
  1514. sys_sleep (timeval)
  1515.      int timeval;
  1516. {
  1517.   int time [2];
  1518.   
  1519.   LIB$EMUL (&timeval, &-10000000, &0, time);       /* Convert to VMS format */
  1520.  
  1521.   SYS$CANTIM (1, 0);
  1522.   if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
  1523.     SYS$WAITFR (timer_ef);      /* Wait for timer expiry only */
  1524. }
  1525.  
  1526. init_sigio ()
  1527. {
  1528.   request_sigio ();
  1529. }
  1530.  
  1531. reset_sigio ()
  1532. {
  1533.   unrequest_sigio ();
  1534. }
  1535.  
  1536. request_sigio ()
  1537. {
  1538.   croak ("request sigio");
  1539. }
  1540.  
  1541. unrequest_sigio ()
  1542. {
  1543.   croak ("unrequest sigio");
  1544. }
  1545.  
  1546. #endif /* VMS */
  1547.  
  1548. /* Note that VMS compiler won't accept defined (CANNOT_DUMP).  */
  1549. #ifndef CANNOT_DUMP
  1550. #define NEED_STARTS
  1551. #endif
  1552.  
  1553. #ifndef SYSTEM_MALLOC
  1554. #ifndef NEED_STARTS
  1555. #define NEED_STARTS
  1556. #endif
  1557. #endif
  1558.  
  1559. #ifdef NEED_STARTS
  1560. /* Some systems that cannot dump also cannot implement these.  */
  1561.  
  1562. /*
  1563.  *    Return the address of the start of the text segment prior to
  1564.  *    doing an unexec().  After unexec() the return value is undefined.
  1565.  *    See crt0.c for further explanation and _start().
  1566.  *
  1567.  */
  1568.  
  1569. #ifndef CANNOT_UNEXEC
  1570. char *
  1571. start_of_text ()
  1572. {
  1573. /**
  1574.  **  (sjk)++ Compute text segment from basepage pointers.
  1575.  **/
  1576. #if defined(atarist)
  1577.   return((char *)_base->p_tbase);
  1578. #else
  1579.  
  1580. #ifdef TEXT_START
  1581.   return ((char *) TEXT_START);
  1582. #else
  1583. #ifdef GOULD
  1584.   extern csrt();
  1585.   return ((char *) csrt);
  1586. #else /* not GOULD */
  1587.   extern int _start ();
  1588.   return ((char *) _start);
  1589. #endif /* GOULD */
  1590. #endif /* TEXT_START */
  1591. #endif /* atarist */
  1592. }
  1593. #endif /* not CANNOT_UNEXEC */
  1594.  
  1595. /*
  1596.  *    Return the address of the start of the data segment prior to
  1597.  *    doing an unexec().  After unexec() the return value is undefined.
  1598.  *    See crt0.c for further information and definition of data_start.
  1599.  *
  1600.  *    Apparently, on BSD systems this is etext at startup.  On
  1601.  *    USG systems (swapping) this is highly mmu dependent and
  1602.  *    is also dependent on whether or not the program is running
  1603.  *    with shared text.  Generally there is a (possibly large)
  1604.  *    gap between end of text and start of data with shared text.
  1605.  *
  1606.  *    On Uniplus+ systems with shared text, data starts at a
  1607.  *    fixed address.  Each port (from a given oem) is generally
  1608.  *    different, and the specific value of the start of data can
  1609.  *    be obtained via the UniPlus+ specific "uvar(2)" system call,
  1610.  *    however the method outlined in crt0.c seems to be more portable.
  1611.  *
  1612.  *    Probably what will have to happen when a USG unexec is available,
  1613.  *    at least on UniPlus, is temacs will have to be made unshared so
  1614.  *    that text and data are contiguous.  Then once loadup is complete,
  1615.  *    unexec will produce a shared executable where the data can be
  1616.  *    at the normal shared text boundry and the startofdata variable
  1617.  *    will be patched by unexec to the correct value.
  1618.  *
  1619.  */
  1620.  
  1621. char *
  1622. start_of_data ()
  1623. {
  1624. /**
  1625.  **  (sjk)++ look up in the basepage 
  1626.  **/
  1627. #if defined(atarist)
  1628.   return ((char *) _base->p_dbase);
  1629. #else 
  1630.  
  1631. #ifdef DATA_START
  1632.   return ((char *) DATA_START);
  1633. #else
  1634.   extern int data_start;
  1635.   return ((char *) &data_start);
  1636. #endif
  1637. #endif /* atarist */
  1638. }
  1639. #endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
  1640.  
  1641. #ifndef CANNOT_DUMP
  1642. /* Some systems that cannot dump also cannot implement these.  */
  1643.  
  1644. /*
  1645.  *    Return the address of the end of the text segment prior to
  1646.  *    doing an unexec().  After unexec() the return value is undefined.
  1647.  */
  1648.  
  1649. char *
  1650. end_of_text ()
  1651. {
  1652. /**
  1653.  **  (sjk)++ look up in the basepage and compute it
  1654.  **/
  1655. #if defined(atarist)
  1656.   return((char *)( _base->p_tbase + _base->p_tlen - 1));
  1657. #else 
  1658.  
  1659. #ifdef TEXT_END
  1660.   return ((char *) TEXT_END);
  1661. #else
  1662.   extern int etext;
  1663.   return ((char *) &etext);
  1664. #endif
  1665. #endif /* atarist */
  1666. }
  1667.  
  1668. /*
  1669.  *    Return the address of the end of the data segment prior to
  1670.  *    doing an unexec().  After unexec() the return value is undefined.
  1671.  */
  1672.  
  1673. char *
  1674. end_of_data ()
  1675. {
  1676. /**
  1677.  **  (sjk)++ look it up in the basepage
  1678.  **/
  1679. #if defined(atarist)
  1680.   return((char *) (_base->p_dbase + _base->p_dlen - 1));
  1681. #else
  1682.  
  1683. #ifdef DATA_END
  1684.   return ((char *) DATA_END);
  1685. #else
  1686.   extern int edata;
  1687.   return ((char *) &edata);
  1688. #endif
  1689. #endif /* atarist */
  1690. }
  1691.  
  1692. #endif /* not CANNOT_DUMP */
  1693.  
  1694. /* Get_system_name returns as its value
  1695.  a string for the Lisp function system-name to return. */
  1696.  
  1697. #ifdef BSD4_1
  1698. #include <whoami.h>
  1699. #endif
  1700.  
  1701. #ifdef USG
  1702. /* Can't have this within the function since `static' is #defined to nothing */
  1703. static struct utsname get_system_name_name;
  1704. #endif
  1705.  
  1706. char *
  1707. get_system_name ()
  1708. {
  1709. #ifdef USG
  1710.   uname (&get_system_name_name);
  1711.   return (get_system_name_name.nodename);
  1712. #else /* Not USG */
  1713. #ifdef BSD4_1
  1714.   return sysname;
  1715. #else /* not USG, not 4.1 */
  1716.   static char system_name_saved[32];
  1717. #ifdef VMS
  1718.   char *sp;
  1719.   if ((sp = egetenv("SYS$NODE")) == 0)
  1720.     sp = "vax-vms";
  1721.   else
  1722.     {
  1723.       char *end;
  1724.  
  1725.       if ((end = index (sp, ':')) != 0)
  1726.     *end = '\0';
  1727.     }
  1728.   strcpy (system_name_saved, sp);
  1729. #else /* not VMS */
  1730.   gethostname (system_name_saved, sizeof (system_name_saved));
  1731. #endif /* not VMS */
  1732.   return system_name_saved;
  1733. #endif /* not USG, not 4.1 */
  1734. #endif /* not USG */
  1735. }
  1736.  
  1737. #ifndef HAVE_SELECT
  1738.  
  1739. /* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
  1740.  * Only checks read descriptors.
  1741.  */
  1742. /* How long to wait between checking fds in select */
  1743. #define SELECT_PAUSE 1
  1744. int select_alarmed;
  1745.  
  1746. /* For longjmp'ing back to read_input_waiting.  */
  1747.  
  1748. jmp_buf read_alarm_throw;
  1749.  
  1750. /* Nonzero if the alarm signal should throw back to read_input_waiting.
  1751.    The read_socket_hook function sets this to 1 while it is waiting.  */
  1752.  
  1753. int read_alarm_should_throw;
  1754.  
  1755. select_alarm ()
  1756. {
  1757.   select_alarmed = 1;
  1758. #ifdef BSD4_1
  1759.   sigrelse (SIGALRM);
  1760. #else /* not BSD4_1 */
  1761.   signal (SIGALRM, SIG_IGN);
  1762. #endif /* not BSD4_1 */
  1763.   if (read_alarm_should_throw)
  1764.     longjmp (read_alarm_throw, 1);
  1765. }
  1766.  
  1767. /* Only rfds are checked.  */
  1768. int
  1769. select (nfds, rfds, wfds, efds, timeout)
  1770.      int nfds;
  1771.      int *rfds, *wfds, *efds, *timeout;
  1772. {
  1773.   int ravail = 0, orfds = 0, old_alarm;
  1774.   int timeoutval = timeout ? *timeout : 100000;
  1775.   int *local_timeout = &timeoutval;
  1776.   extern int kbd_count;
  1777. /**
  1778.  **  (sjk)++ Not needed since we do a char at a time.
  1779.  **/
  1780. #if !defined(atarist)
  1781.   extern int proc_buffered_char[];
  1782. #endif
  1783.  
  1784. #ifndef subprocesses
  1785.   int process_tick = 0, update_tick = 0;
  1786. #else
  1787.   extern int process_tick, update_tick;
  1788. #endif
  1789.   int (*old_trap) ();
  1790.   char buf;
  1791.  
  1792.   if (rfds)
  1793.     {
  1794.       orfds = *rfds;
  1795.       *rfds = 0;
  1796.     }
  1797.   if (wfds)
  1798.     *wfds = 0;
  1799.   if (efds)
  1800.     *efds = 0;
  1801.  
  1802.   /* If we are looking only for the terminal, with no timeout,
  1803.      just read it and wait -- that's more efficient.  */
  1804.   if (orfds == 1 && (!timeout || *timeout == 100000)
  1805.       && process_tick == update_tick)
  1806.     {
  1807.       if (!kbd_count)
  1808.     read_input_waiting ();
  1809.       *rfds = 1;
  1810.       return 1;
  1811.     }
  1812.  
  1813.   /* Once a second, till the timer expires, check all the flagged read
  1814.    * descriptors to see if any input is available.  If there is some then
  1815.    * set the corresponding bit in the return copy of rfds.
  1816.    */ 
  1817.   while (1)
  1818.     {
  1819.       register int to_check, bit, fd;
  1820.  
  1821.       if (rfds)
  1822.     {
  1823.       for (to_check = nfds, bit = 1, fd = 0; --to_check >= 0; bit <<= 1, fd++)
  1824.         {
  1825.           if (orfds & bit)
  1826.         {
  1827.           int avail = 0, status = 0;
  1828.  
  1829.           if (bit == 1)
  1830.             avail = detect_input_pending(); /* Special keyboard handler */
  1831.           else
  1832.             {
  1833. #ifdef FIONREAD
  1834.               status = ioctl (fd, FIONREAD, &avail);
  1835. #else /* no FIONREAD */
  1836.               /* Hoping it will return -1 if nothing available
  1837.              or 0 if all 0 chars requested are read.  */
  1838. /**
  1839.  **  (sjk)++ not needed since we do a char at a time.
  1840.  **/
  1841. #if !defined(atarist)
  1842.               if (proc_buffered_char[fd] >= 0)
  1843.             avail = 1;
  1844.               else
  1845.             {
  1846.               avail = read (fd, &buf, 1);
  1847.               if (avail > 0)
  1848.                 proc_buffered_char[fd] = buf;
  1849.             }
  1850. #endif /* atarist */
  1851. #endif /* no FIONREAD */
  1852.             }
  1853.           if (status >= 0 && avail > 0)
  1854.             {
  1855.               (*rfds) |= bit;
  1856.               ravail++;
  1857.             }
  1858.         }
  1859.         }
  1860.     }
  1861.       if (*local_timeout == 0 || ravail != 0 || process_tick != update_tick)
  1862.     break;
  1863.       old_alarm = alarm (0);
  1864. /**
  1865.  **  (sjk)++ typecast the signal
  1866.  **/
  1867. #if defined(atarist)
  1868.       old_trap = (int (*)()) signal (SIGALRM, (__Sigfunc)select_alarm);
  1869. #else
  1870.       old_trap = (int (*)()) signal (SIGALRM, select_alarm);
  1871. #endif
  1872.  
  1873.       select_alarmed = 0;
  1874.       alarm (SELECT_PAUSE);
  1875.       /* Wait for a SIGALRM (or maybe a SIGTINT) */
  1876.       while (select_alarmed == 0 && *local_timeout != 0
  1877.          && process_tick == update_tick)
  1878.     {
  1879.       /* If we are interested in terminal input,
  1880.          wait by reading the terminal.
  1881.          That makes instant wakeup for terminal input at least.  */
  1882.       if (orfds & 1)
  1883.         {
  1884.           read_input_waiting ();
  1885.           if (kbd_count)
  1886.         select_alarmed = 1;
  1887.         }
  1888.       else
  1889.         pause();
  1890.     }
  1891.       (*local_timeout) -= SELECT_PAUSE;
  1892.       /* Reset the old alarm if there was one */
  1893.       alarm (0);
  1894. /**
  1895.  **  (sjk)++ typecast the signal
  1896.  **/
  1897. #if defined(atarist)
  1898.       signal (SIGALRM, (__Sigfunc)old_trap);
  1899. #else
  1900.       signal (SIGALRM, old_trap);
  1901. #endif
  1902.       if (old_alarm != 0)
  1903.     {
  1904.       /* Reset or forge an interrupt for the original handler. */
  1905.       old_alarm -= SELECT_PAUSE;
  1906.       if (old_alarm <= 0)
  1907.         kill (getpid (), SIGALRM); /* Fake an alarm with the orig' handler */
  1908.       else
  1909.         alarm (old_alarm);
  1910.     }
  1911.       if (*local_timeout == 0)  /* Stop on timer being cleared */
  1912.     break;
  1913.     }
  1914.   return ravail;
  1915. }
  1916.  
  1917. /* Read keyboard input into the standard buffer,
  1918.    waiting for at least one character.  */
  1919.  
  1920. /* Make all keyboard buffers much bigger when using X windows.  */
  1921. #ifdef HAVE_X_WINDOWS
  1922. #define BUFFER_SIZE_FACTOR 16
  1923. #else
  1924. #define BUFFER_SIZE_FACTOR 1
  1925. #endif
  1926.  
  1927. read_input_waiting ()
  1928. {
  1929.   extern int kbd_count;
  1930.   extern unsigned char kbd_buffer[];
  1931.   extern unsigned char *kbd_ptr;
  1932.   int val;
  1933.  
  1934.   if (read_socket_hook)
  1935.     {
  1936.       read_alarm_should_throw = 0;
  1937.       if (! setjmp (read_alarm_throw))
  1938.     val = (*read_socket_hook) (0, kbd_buffer, 256 * BUFFER_SIZE_FACTOR);
  1939.       else
  1940.     val = -1;
  1941.     }
  1942.   else
  1943.     val = read (fileno (stdin), kbd_buffer, 1);
  1944.  
  1945.   if (val > 0)
  1946.     {
  1947.       kbd_ptr = kbd_buffer;
  1948.       kbd_count = val;
  1949.     }
  1950. }
  1951.  
  1952. #endif /* not HAVE_SELECT */
  1953.  
  1954. #ifdef BSD4_1
  1955. /* VARARGS */
  1956. setpriority ()
  1957. {
  1958.   return 0;
  1959. }
  1960.  
  1961. /*
  1962.  * Partially emulate 4.2 open call.
  1963.  * open is defined as this in 4.1.
  1964.  *
  1965.  * - added by Michael Bloom @ Citicorp/TTI
  1966.  *
  1967.  */
  1968.  
  1969. int
  1970. sys_open (path, oflag, mode)
  1971.      char *path;
  1972.      int oflag, mode;
  1973. {
  1974.   if (oflag & O_CREAT) 
  1975.     return creat (path, mode);
  1976.   else
  1977.     return open (path, oflag);
  1978. }
  1979.  
  1980. init_sigio ()
  1981. {
  1982.   if (noninteractive)
  1983.     return;
  1984.   lmode = LINTRUP | lmode;
  1985. /**
  1986.  **  (sjk)++ No local characters on he ST.
  1987.  **/
  1988. #if !defined(atarist)
  1989.   ioctl (0, TIOCLSET, &lmode);
  1990. #endif
  1991. }
  1992.  
  1993. reset_sigio ()
  1994. {
  1995.   if (noninteractive)
  1996.     return;
  1997.   lmode = ~LINTRUP & lmode;
  1998. /**
  1999.  **  (sjk)++ No local characters on the ST.
  2000.  **/
  2001. #if !defined(atarist)
  2002.   ioctl (0, TIOCLSET, &lmode);
  2003. #endif
  2004. }
  2005.  
  2006. request_sigio ()
  2007. {
  2008.   sigrelse (SIGTINT);
  2009.  
  2010.   interrupts_deferred = 0;
  2011. }
  2012.  
  2013. unrequest_sigio ()
  2014. {
  2015.   sighold (SIGTINT);
  2016.  
  2017.   interrupts_deferred = 1;
  2018. }
  2019.  
  2020. /* still inside #ifdef BSD4_1 */
  2021. #ifdef subprocesses
  2022.  
  2023. int sigheld; /* Mask of held signals */
  2024.  
  2025. sigholdx (signum)
  2026.      int signum;
  2027. {
  2028.   sigheld |= sigbit (signum);
  2029.   sighold (signum);
  2030. }
  2031.  
  2032. sigisheld (signum)
  2033.      int signum;
  2034. {
  2035.   sigheld |= sigbit (signum);
  2036. }
  2037.  
  2038. sigunhold (signum)
  2039.      int signum;
  2040. {
  2041.   sigheld &= ~sigbit (signum);
  2042.   sigrelse (signum);
  2043. }
  2044.  
  2045. sigfree ()    /* Free all held signals */
  2046. {
  2047.   int i;
  2048.   for (i = 0; i < NSIG; i++)
  2049.     if (sigheld & sigbit (i))
  2050.       sigrelse (i);
  2051.   sigheld = 0;
  2052. }
  2053.  
  2054. sigbit (i)
  2055. {
  2056.   return 1 << (i - 1);
  2057. }
  2058. #endif /* subprocesses */
  2059. #endif /* BSD4_1 */
  2060.  
  2061. #ifndef BSTRING
  2062.  
  2063. void
  2064. bzero (b, length)
  2065.      register char *b;
  2066.      register int length;
  2067. {
  2068. #ifdef VMS
  2069.   short zero = 0;
  2070.   long max_str = 65535;
  2071.  
  2072.   while (length > max_str) {
  2073.     (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
  2074.     length -= max_str;
  2075.     b += max_str;
  2076.   }
  2077.   (void) LIB$MOVC5 (&zero, &zero, &zero, &length, b);
  2078. #else
  2079.   while (length-- > 0)
  2080.     *b++ = 0;
  2081. #endif /* not VMS */
  2082. }
  2083.  
  2084. /* Saying `void' requires a declaration, above, where bcopy is used
  2085.    and that declaration causes pain for systems where bcopy is a macro.  */
  2086. bcopy (b1, b2, length)
  2087.      register char *b1;
  2088.      register char *b2;
  2089.      register int length;
  2090. {
  2091. #ifdef VMS
  2092.   long max_str = 65535;
  2093.  
  2094.   while (length > max_str) {
  2095.     (void) LIB$MOVC3 (&max_str, b1, b2);
  2096.     length -= max_str;
  2097.     b1 += max_str;
  2098.     b2 += max_str;
  2099.   }
  2100.   (void) LIB$MOVC3 (&length, b1, b2);
  2101. #else
  2102.   while (length-- > 0)
  2103.     *b2++ = *b1++;
  2104. #endif /* not VMS */
  2105. }
  2106.  
  2107. int
  2108. bcmp (b1, b2, length)    /* This could be a macro! */
  2109.      register char *b1;
  2110.      register char *b2;
  2111.      register int length;
  2112. {
  2113. #ifdef VMS
  2114.   struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
  2115.   struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
  2116.  
  2117.   return STR$COMPARE (&src1, &src2);
  2118. #else
  2119.   while (length-- > 0)
  2120.     if (*b1++ != *b2++)
  2121.       return 1;
  2122.  
  2123.   return 0;
  2124. #endif /* not VMS */
  2125. }
  2126. #endif /* not BSTRING */
  2127.  
  2128. /**
  2129.  **  (sjk)++ redefine this to make our life easy.
  2130.  **/
  2131. #ifdef atarist
  2132. long random ()
  2133. {
  2134.   return (rand ());
  2135. }
  2136.  
  2137. srandom (arg)
  2138.      int arg;
  2139. {
  2140.   srand (arg);
  2141. }
  2142. #endif /* atarist */
  2143.  
  2144. #ifdef BSD4_1
  2145. long random ()
  2146. {
  2147.   return (rand ());
  2148. }
  2149.  
  2150. srandom (arg)
  2151.      int arg;
  2152. {
  2153.   srand (arg);
  2154. }
  2155. #endif BSD4_1
  2156.  
  2157. #ifdef HPUX
  2158. #ifdef X11
  2159. #define HAVE_RANDOM
  2160. #endif
  2161. #endif
  2162.  
  2163. #ifdef USG
  2164. #ifndef HAVE_RANDOM
  2165. /*
  2166.  *    The BSD random(3) returns numbers in the range of
  2167.  *    0 to 2e31 - 1.  The USG rand(3C) returns numbers in the
  2168.  *    range of 0 to 2e15 - 1.  This is probably not significant
  2169.  *    in this usage.
  2170.  */
  2171.   
  2172. long
  2173. random ()
  2174. {
  2175.   /* Arrange to return a range centered on zero.  */
  2176.   return rand () - (1 << 14);
  2177. }
  2178.  
  2179. srandom (arg)
  2180.      int arg;
  2181. {
  2182.   srand (arg);
  2183. }
  2184.  
  2185. #endif /* HAVE_RANDOM */
  2186. #endif /* USG */
  2187.  
  2188.  
  2189. #ifdef VMS
  2190.  
  2191. #ifdef getenv
  2192. /* If any place else asks for the TERM variable,
  2193.    allow it to be overridden with the EMACS_TERM variable
  2194.    before attempting to translate the logical name TERM.  As a last
  2195.    resort, ask for VAX C's special idea of the TERM variable.  */
  2196. #undef getenv
  2197. char *
  2198. sys_getenv (name)
  2199.      char *name;
  2200. {
  2201.   register char *val;
  2202.   static char buf[256];
  2203.   static struct dsc$descriptor_s equiv
  2204.     = {sizeof(buf), DSC$K_DTYPE_T, DSC$K_CLASS_S, buf};
  2205.   static struct dsc$descriptor_s d_name
  2206.     = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
  2207.   short eqlen;
  2208.  
  2209.   if (!strcmp (name, "TERM"))
  2210.     {
  2211.       val = (char *) getenv ("EMACS_TERM");
  2212.       if (val)
  2213.     return val;
  2214.     }
  2215.  
  2216.   d_name.dsc$w_length = strlen (name);
  2217.   d_name.dsc$a_pointer = name;
  2218.   if (lib$sys_trnlog (&d_name, &eqlen, &equiv) == 1)
  2219.     {
  2220.       char *str = (char *) xmalloc (eqlen + 1);
  2221.       bcopy (buf, str, eqlen);
  2222.       str[eqlen] = '\0';
  2223.       /* This is a storage leak, but a pain to fix.  With luck,
  2224.      no one will ever notice.  */
  2225.       return str;
  2226.     }
  2227.   return (char *) getenv (name);
  2228. }
  2229. #endif /* getenv */
  2230.  
  2231. #ifdef abort
  2232. /* Since VMS doesn't believe in core dumps, the only way to debug this beast is
  2233.    to force a call on the debugger from within the image. */
  2234. #undef abort
  2235. sys_abort ()
  2236. {
  2237.   reset_sys_modes ();
  2238.   LIB$SIGNAL (SS$_DEBUG);
  2239. }
  2240. #endif /* abort */
  2241. #endif /* VMS */
  2242.  
  2243. #ifdef VMS
  2244. #ifdef LINK_CRTL_SHARE
  2245. #ifdef SHAREABLE_LIB_BUG
  2246. /* Variables declared noshare and initialized in shareable libraries
  2247.    cannot be shared.  The VMS linker incorrectly forces you to use a private
  2248.    version which is uninitialized... If not for this "feature", we
  2249.    could use the C library definition of sys_nerr and sys_errlist. */
  2250. int sys_nerr = 35;
  2251. char *sys_errlist[] =
  2252.   {
  2253.     "error 0",
  2254.     "not owner",
  2255.     "no such file or directory",
  2256.     "no such process",
  2257.     "interrupted system call",
  2258.     "i/o error",
  2259.     "no such device or address",
  2260.     "argument list too long",
  2261.     "exec format error",
  2262.     "bad file number",
  2263.     "no child process",
  2264.     "no more processes",
  2265.     "not enough memory",
  2266.     "permission denied",
  2267.     "bad address",
  2268.     "block device required",
  2269.     "mount devices busy",
  2270.     "file exists",
  2271.     "cross-device link",
  2272.     "no such device",
  2273.     "not a directory",
  2274.     "is a directory",
  2275.     "invalid argument",
  2276.     "file table overflow",
  2277.     "too many open files",
  2278.     "not a typewriter",
  2279.     "text file busy",
  2280.     "file too big",
  2281.     "no space left on device",
  2282.     "illegal seek",
  2283.     "read-only file system",
  2284.     "too many links",
  2285.     "broken pipe",
  2286.     "math argument",
  2287.     "result too large",
  2288.     "I/O stream empty",
  2289.     "vax/vms specific error code nontranslatable error"
  2290.   };
  2291. #endif /* SHAREABLE_LIB_BUG */
  2292. #endif /* LINK_CRTL_SHARE */
  2293. #endif /* VMS */
  2294.  
  2295. #ifdef INTERRUPTABLE_OPEN
  2296.  
  2297. int
  2298. /* VARARGS 2 */
  2299. sys_open (path, oflag, mode)
  2300.      char *path;
  2301.      int oflag, mode;
  2302. {
  2303.   register int rtnval;
  2304.   
  2305.   while ((rtnval = open (path, oflag, mode)) == -1
  2306.      && (errno == EINTR));
  2307.   return (rtnval);
  2308. }
  2309.  
  2310. #endif /* INTERRUPTABLE_OPEN */
  2311.  
  2312. #ifdef INTERRUPTABLE_CLOSE
  2313.  
  2314. sys_close (fd)
  2315.      int fd;
  2316. {
  2317.   register int rtnval;
  2318.  
  2319.   while ((rtnval = close(fd)) == -1
  2320.      && (errno == EINTR));
  2321.   return rtnval;
  2322. }
  2323.  
  2324. #endif /* INTERRUPTABLE_CLOSE */
  2325.  
  2326. #ifdef INTERRUPTABLE_IO
  2327.  
  2328. int
  2329. sys_read (fildes, buf, nbyte)
  2330.      int fildes;
  2331.      char *buf;
  2332.      unsigned int nbyte;
  2333. {
  2334.   register int rtnval;
  2335.   
  2336.   while ((rtnval = read (fildes, buf, nbyte)) == -1
  2337.      && (errno == EINTR));
  2338.   return (rtnval);
  2339. }
  2340.  
  2341. int
  2342. sys_write (fildes, buf, nbyte)
  2343.      int fildes;
  2344.      char *buf;
  2345.      unsigned int nbyte;
  2346. {
  2347.   register int rtnval;
  2348.  
  2349.   while ((rtnval = write (fildes, buf, nbyte)) == -1
  2350.      && (errno == EINTR));
  2351.   return (rtnval);
  2352. }
  2353.  
  2354. #endif /* INTERRUPTABLE_IO */
  2355.  
  2356. #ifdef USG
  2357. /*
  2358.  *    All of the following are for USG.
  2359.  *
  2360.  *    On USG systems the system calls are interruptable by signals
  2361.  *    that the user program has elected to catch.  Thus the system call
  2362.  *    must be retried in these cases.  To handle this without massive
  2363.  *    changes in the source code, we remap the standard system call names
  2364.  *    to names for our own functions in sysdep.c that do the system call
  2365.  *    with retries.  Actually, for portability reasons, it is good
  2366.  *    programming practice, as this example shows, to limit all actual
  2367.  *    system calls to a single occurance in the source.  Sure, this
  2368.  *    adds an extra level of function call overhead but it is almost
  2369.  *    always negligible.   Fred Fish, Unisoft Systems Inc.
  2370.  */
  2371.  
  2372. char *sys_siglist[NSIG + 1] =
  2373. {
  2374. #ifdef AIX
  2375. /* AIX has changed the signals a bit */
  2376.   "bogus signal",            /* 0 */
  2377.   "hangup",                /* 1  SIGHUP */
  2378.   "interrupt",                /* 2  SIGINT */
  2379.   "quit",                /* 3  SIGQUIT */
  2380.   "illegal instruction",        /* 4  SIGILL */
  2381.   "trace trap",                /* 5  SIGTRAP */
  2382.   "IOT instruction",            /* 6  SIGIOT */
  2383.   "crash likely",            /* 7  SIGDANGER */
  2384.   "floating point exception",        /* 8  SIGFPE */
  2385.   "kill",                /* 9  SIGKILL */
  2386.   "bus error",                /* 10 SIGBUS */
  2387.   "segmentation violation",        /* 11 SIGSEGV */
  2388.   "bad argument to system call",    /* 12 SIGSYS */
  2389.   "write on a pipe with no one to read it", /* 13 SIGPIPE */
  2390.   "alarm clock",            /* 14 SIGALRM */
  2391.   "software termination signum",    /* 15 SIGTERM */
  2392.   "user defined signal 1",        /* 16 SIGUSR1 */
  2393.   "user defined signal 2",        /* 17 SIGUSR2 */
  2394.   "death of a child",            /* 18 SIGCLD */
  2395.   "power-fail restart",            /* 19 SIGPWR */
  2396.   "bogus signal",            /* 20 */
  2397.   "bogus signal",            /* 21 */
  2398.   "bogus signal",            /* 22 */
  2399.   "bogus signal",            /* 23 */
  2400.   "bogus signal",            /* 24 */
  2401.   "LAN I/O interrupt",            /* 25 SIGAIO */
  2402.   "PTY I/O interrupt",            /* 26 SIGPTY */
  2403.   "I/O intervention required",        /* 27 SIGIOINT */
  2404.   "HFT grant",                /* 28 SIGGRANT */
  2405.   "HFT retract",            /* 29 SIGRETRACT */
  2406.   "HFT sound done",            /* 30 SIGSOUND */
  2407.   "HFT input ready",            /* 31 SIGMSG */
  2408. #else /* not AIX */
  2409.   "bogus signal",            /* 0 */
  2410.   "hangup",                /* 1  SIGHUP */
  2411.   "interrupt",                /* 2  SIGINT */
  2412.   "quit",                /* 3  SIGQUIT */
  2413.   "illegal instruction",        /* 4  SIGILL */
  2414.   "trace trap",                /* 5  SIGTRAP */
  2415.   "IOT instruction",            /* 6  SIGIOT */
  2416.   "EMT instruction",            /* 7  SIGEMT */
  2417.   "floating point exception",        /* 8  SIGFPE */
  2418.   "kill",                /* 9  SIGKILL */
  2419.   "bus error",                /* 10 SIGBUS */
  2420.   "segmentation violation",        /* 11 SIGSEGV */
  2421.   "bad argument to system call",    /* 12 SIGSYS */
  2422.   "write on a pipe with no one to read it", /* 13 SIGPIPE */
  2423.   "alarm clock",            /* 14 SIGALRM */
  2424.   "software termination signum",    /* 15 SIGTERM */
  2425.   "user defined signal 1",        /* 16 SIGUSR1 */
  2426.   "user defined signal 2",        /* 17 SIGUSR2 */
  2427.   "death of a child",            /* 18 SIGCLD */
  2428.   "power-fail restart",            /* 19 SIGPWR */
  2429. #endif /* not AIX */
  2430.   0
  2431.   };
  2432.  
  2433. /*
  2434.  *    Warning, this function may not duplicate 4.2 action properly
  2435.  *    under error conditions.
  2436.  */
  2437.  
  2438. #ifndef MAXPATHLEN
  2439. /* In 4.1, param.h fails to define this.  */
  2440. #define MAXPATHLEN 1024
  2441. #endif
  2442.  
  2443. #ifndef HAVE_GETWD
  2444.  
  2445. char *
  2446. getwd (pathname)
  2447.      char *pathname;
  2448. {
  2449.   char *npath, *spath;
  2450.   extern char *getcwd ();
  2451.  
  2452.   spath = npath = getcwd ((char *) 0, MAXPATHLEN);
  2453.   /* On Altos 3068, getcwd can return @hostname/dir, so discard
  2454.      up to first slash.  Should be harmless on other systems.  */
  2455.   while (*npath && *npath != '/')
  2456.     npath++;
  2457.   strcpy (pathname, npath);
  2458.   free (spath);            /* getcwd uses malloc */
  2459.   return pathname;
  2460. }
  2461.  
  2462. #endif HAVE_GETWD
  2463.  
  2464. #ifndef HAVE_RENAME
  2465.  
  2466. /*
  2467.  *    Emulate rename using unlink/link.  Note that this is
  2468.  *    only partially correct.  Also, doesn't enforce restriction
  2469.  *    that files be of same type (regular->regular, dir->dir, etc).
  2470.  */
  2471.  
  2472. rename (from, to)
  2473.      char *from;
  2474.      char *to;
  2475. {
  2476.   if (access (from, 0) == 0)
  2477.     {
  2478.       unlink (to);
  2479.       if (link (from, to) == 0)
  2480.     if (unlink (from) == 0)
  2481.       return (0);
  2482.     }
  2483.   return (-1);
  2484. }
  2485. #endif /* not HAVE_RENAME */
  2486.  
  2487. /* VARARGS */
  2488. setpriority ()
  2489. {
  2490.   return (0);
  2491. }
  2492.  
  2493. #ifndef HAVE_VFORK
  2494.  
  2495. /*
  2496.  *    Substitute fork(2) for vfork(2) on USG flavors.
  2497.  */
  2498.  
  2499. vfork ()
  2500. {
  2501.   return (fork ());
  2502. }
  2503.  
  2504. #endif /* not HAVE_VFORK */
  2505.  
  2506. #ifdef MISSING_UTIMES
  2507.  
  2508. /* HPUX (among others) sets HAVE_TIMEVAL but does not implement utimes.  */
  2509.  
  2510. utimes ()
  2511. {
  2512. }
  2513. #endif
  2514.  
  2515. #ifdef IRIS_UTIME
  2516.  
  2517. /* The IRIS (3.5) has timevals, but uses sys V utime, and doesn't have the
  2518.    utimbuf structure defined anywhere but in the man page. */
  2519.  
  2520. struct utimbuf
  2521.  {
  2522.    long actime;
  2523.    long modtime;
  2524.  };
  2525.  
  2526. utimes (name, tvp)
  2527.      char *name;
  2528.      struct timeval tvp[];
  2529. {
  2530.   struct utimbuf utb;
  2531.   utb.actime  = tvp[0].tv_sec;
  2532.   utb.modtime = tvp[1].tv_sec;
  2533.   utime (name, &utb);
  2534. }
  2535. #endif /* IRIS_UTIME */
  2536.  
  2537.  
  2538. #if 0
  2539. #ifdef HPUX
  2540.  
  2541. /* HPUX curses library references perror, but as far as we know
  2542.    it won't be called.  Anyway this definition will do for now.  */
  2543.  
  2544. perror ()
  2545. {
  2546. }
  2547.  
  2548. #endif /* HPUX */
  2549. #endif /* 0 */
  2550.  
  2551. #ifndef HAVE_DUP2
  2552.  
  2553. /*
  2554.  *    Emulate BSD dup2(2).  First close newd if it already exists.
  2555.  *    Then, attempt to dup oldd.  If not successful, call dup2 recursively
  2556.  *    until we are, then close the unsuccessful ones.
  2557.  */
  2558.  
  2559. dup2 (oldd, newd)
  2560.      int oldd;
  2561.      int newd;
  2562. {
  2563.   register int fd;
  2564.   
  2565.   sys_close (newd);
  2566.  
  2567. #ifdef F_DUPFD
  2568.   fd = fcntl (oldd, F_DUPFD, newd);
  2569.   if (fd != newd)
  2570.     error ("cant dup2(%i,%i) : %s", oldd, newd, sys_errlist[errno]);
  2571. #else
  2572.   while ((fd = dup (oldd)) != newd)
  2573.     {
  2574.       dup2 (oldd, newd);
  2575.       sys_close (fd);
  2576.     }
  2577. #endif
  2578. }
  2579.  
  2580. #endif /* not HAVE_DUP2 */
  2581.  
  2582. /*
  2583.  *    Gettimeofday.  Simulate as much as possible.  Only accurate
  2584.  *    to nearest second.  Emacs doesn't use tzp so ignore it for now.
  2585.  *    Only needed when subprocesses are defined.
  2586.  */
  2587.  
  2588. #ifdef subprocesses
  2589. #ifndef HAVE_GETTIMEOFDAY
  2590. #ifdef HAVE_TIMEVAL
  2591.  
  2592. /* ARGSUSED */
  2593. gettimeofday (tp, tzp)
  2594.      struct timeval *tp;
  2595.      struct timezone *tzp;
  2596. {
  2597.   extern long time ();
  2598.  
  2599.   tp->tv_sec = time ((long *)0);    
  2600.   tp->tv_usec = 0;
  2601. }
  2602.  
  2603. #endif
  2604. #endif
  2605. #endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL */
  2606.   
  2607. /*
  2608.  *    This function will go away as soon as all the stubs fixed. (fnf)
  2609.  */
  2610.  
  2611. croak (badfunc)
  2612.      char *badfunc;
  2613. {
  2614.   printf ("%s not yet implemented\r\n", badfunc);
  2615.   reset_sys_modes ();
  2616.   exit (1);
  2617. }
  2618.  
  2619. #endif /* USG */
  2620.  
  2621. /* Directory routines for systems that don't have them. */
  2622.  
  2623. #ifdef SYSV_SYSTEM_DIR
  2624.  
  2625. #include <dirent.h>
  2626.  
  2627. #ifndef HAVE_CLOSEDIR
  2628. int
  2629. closedir (dirp)
  2630.      register DIR *dirp;              /* stream from opendir() */
  2631. {
  2632.   sys_close (dirp->dd_fd);
  2633.   free ((char *) dirp->dd_buf);       /* directory block defined in <dirent.h> */
  2634.   free ((char *) dirp);
  2635. }
  2636. #endif /* not HAVE_CLOSEDIR */
  2637.  
  2638. #endif /* SYSV_SYSTEM_DIR */
  2639.  
  2640. #ifdef NONSYSTEM_DIR_LIBRARY
  2641.  
  2642. DIR *
  2643. opendir (filename)
  2644.      char *filename;    /* name of directory */
  2645. {
  2646.   register DIR *dirp;        /* -> malloc'ed storage */
  2647.   register int fd;        /* file descriptor for read */
  2648.   struct stat sbuf;        /* result of fstat() */
  2649.  
  2650.   fd = sys_open (filename, 0);
  2651.   if (fd < 0)
  2652.     return 0;
  2653.  
  2654.   if (fstat (fd, &sbuf) < 0
  2655.       || (sbuf.st_mode & S_IFMT) != S_IFDIR
  2656.       || (dirp = (DIR *) malloc (sizeof (DIR))) == 0)
  2657.     {
  2658.       sys_close (fd);
  2659.       return 0;        /* bad luck today */
  2660.     }
  2661.  
  2662.   dirp->dd_fd = fd;
  2663.   dirp->dd_loc = dirp->dd_size = 0;    /* refill needed */
  2664.  
  2665.   return dirp;
  2666. }
  2667.  
  2668. void
  2669. closedir (dirp)
  2670.      register DIR *dirp;        /* stream from opendir() */
  2671. {
  2672.   sys_close (dirp->dd_fd);
  2673.   free ((char *) dirp);
  2674. }
  2675.  
  2676.  
  2677. #ifndef VMS
  2678. #define DIRSIZ    14
  2679. struct olddir
  2680.   {
  2681.     ino_t od_ino;         /* inode */
  2682.     char od_name[DIRSIZ];    /* filename */
  2683.   };
  2684. #endif /* not VMS */
  2685.  
  2686. struct direct dir_static;    /* simulated directory contents */
  2687.  
  2688. /* ARGUSED */
  2689. struct direct *
  2690. readdir (dirp)
  2691.      register DIR *dirp;    /* stream from opendir() */
  2692. {
  2693. #ifndef VMS
  2694.   register struct olddir *dp;    /* -> directory data */
  2695. #else /* VMS */
  2696.   register struct dir$_name *dp; /* -> directory data */
  2697.   register struct dir$_version *dv; /* -> version data */
  2698. #endif /* VMS */
  2699.  
  2700.   for (; ;)
  2701.     {
  2702.       if (dirp->dd_loc >= dirp->dd_size)
  2703.     dirp->dd_loc = dirp->dd_size = 0;
  2704.  
  2705.       if (dirp->dd_size == 0     /* refill buffer */
  2706.       && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
  2707.     return 0;
  2708.  
  2709. #ifndef VMS
  2710.       dp = (struct olddir *) &dirp->dd_buf[dirp->dd_loc];
  2711.       dirp->dd_loc += sizeof (struct olddir);
  2712.  
  2713.       if (dp->od_ino != 0)    /* not deleted entry */
  2714.     {
  2715.       dir_static.d_ino = dp->od_ino;
  2716.       strncpy (dir_static.d_name, dp->od_name, DIRSIZ);
  2717.       dir_static.d_name[DIRSIZ] = '\0';
  2718.       dir_static.d_namlen = strlen (dir_static.d_name);
  2719.       dir_static.d_reclen = sizeof (struct direct)
  2720.         - MAXNAMLEN + 3
  2721.           + dir_static.d_namlen - dir_static.d_namlen % 4;
  2722.       return &dir_static;    /* -> simulated structure */
  2723.     }
  2724. #else /* VMS */
  2725.       dp = (struct dir$_name *) dirp->dd_buf;
  2726.       if (dirp->dd_loc == 0)
  2727.     dirp->dd_loc = (dp->dir$b_namecount&1) ? dp->dir$b_namecount + 1
  2728.       : dp->dir$b_namecount;
  2729.       dv = (struct dir$_version *)&dp->dir$t_name[dirp->dd_loc];
  2730.       dir_static.d_ino = dv->dir$w_fid_num;
  2731.       dir_static.d_namlen = dp->dir$b_namecount;
  2732.       dir_static.d_reclen = sizeof (struct direct)
  2733.     - MAXNAMLEN + 3
  2734.       + dir_static.d_namlen - dir_static.d_namlen % 4;
  2735.       strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
  2736.       dir_static.d_name[dir_static.d_namlen] = '\0';
  2737.       dirp->dd_loc = dirp->dd_size; /* only one record at a time */
  2738.       return &dir_static;
  2739. #endif /* VMS */
  2740.     }
  2741. }
  2742.  
  2743. #ifdef VMS
  2744. /* readdirver is just like readdir except it returns all versions of a file
  2745.    as separate entries.  */
  2746.  
  2747. /* ARGUSED */
  2748. struct direct *
  2749. readdirver (dirp)
  2750.      register DIR *dirp;    /* stream from opendir() */
  2751. {
  2752.   register struct dir$_name *dp; /* -> directory data */
  2753.   register struct dir$_version *dv; /* -> version data */
  2754.  
  2755.   if (dirp->dd_loc >= dirp->dd_size - sizeof (struct dir$_name))
  2756.     dirp->dd_loc = dirp->dd_size = 0;
  2757.  
  2758.   if (dirp->dd_size == 0     /* refill buffer */
  2759.       && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
  2760.     return 0;
  2761.  
  2762.   dp = (struct dir$_name *) dirp->dd_buf;
  2763.   if (dirp->dd_loc == 0)
  2764.     dirp->dd_loc = (dp->dir$b_namecount & 1) ? dp->dir$b_namecount + 1
  2765.            : dp->dir$b_namecount;
  2766.   dv = (struct dir$_version *) &dp->dir$t_name[dirp->dd_loc];
  2767.   strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
  2768.   sprintf (&dir_static.d_name[dp->dir$b_namecount], ";%d", dv->dir$w_version);
  2769.   dir_static.d_namlen = strlen (dir_static.d_name);
  2770.   dir_static.d_ino = dv->dir$w_fid_num;
  2771.   dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3
  2772.             + dir_static.d_namlen - dir_static.d_namlen % 4;
  2773.   dirp->dd_loc = ((char *) (++dv) - dp->dir$t_name);
  2774.   return &dir_static;
  2775. }
  2776.  
  2777. #endif /* VMS */
  2778.  
  2779. #endif /* NONSYSTEM_DIR_LIBRARY */
  2780.  
  2781. /* Functions for VMS */
  2782. #ifdef VMS
  2783. #include <pwd.h>
  2784. #include <acldef.h>
  2785. #include <chpdef.h>
  2786. #include <jpidef.h>
  2787.  
  2788. /* Return as a string the VMS error string pertaining to STATUS.
  2789.    Reuses the same static buffer each time it is called.  */
  2790.  
  2791. char *
  2792. vmserrstr (status)
  2793.      int status;        /* VMS status code */
  2794. {
  2795.   int bufadr[2];
  2796.   short len;
  2797.   static char buf[257];
  2798.  
  2799.   bufadr[0] = sizeof buf - 1;
  2800.   bufadr[1] = buf;
  2801.   if (! (SYS$GETMSG (status, &len, bufadr, 0x1, 0) & 1))
  2802.     return "untranslatable VMS error status";
  2803.   buf[len] = '\0';
  2804.   return buf;
  2805. }
  2806.  
  2807. #ifdef access
  2808. #undef access
  2809.   
  2810. /* The following is necessary because 'access' emulation by VMS C (2.0) does
  2811.  * not work correctly.  (It also doesn't work well in version 2.3.)
  2812.  */
  2813.  
  2814. #ifdef VMS4_4
  2815.  
  2816. #define DESCRIPTOR(name,string) struct dsc$descriptor_s name = \
  2817.     { strlen(string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string }
  2818.  
  2819. typedef union {
  2820.     struct {
  2821.     unsigned short s_buflen;
  2822.     unsigned short s_code;
  2823.     char *s_bufadr;
  2824.     unsigned short *s_retlenadr;
  2825.     } s;
  2826.     int end;
  2827. } item;
  2828. #define buflen s.s_buflen
  2829. #define code s.s_code
  2830. #define bufadr s.s_bufadr
  2831. #define retlenadr s.s_retlenadr
  2832.  
  2833. #define R_OK 4    /* test for read permission */
  2834. #define W_OK 2    /* test for write permission */
  2835. #define X_OK 1    /* test for execute (search) permission */
  2836. #define F_OK 0    /* test for presence of file */
  2837.  
  2838. int
  2839. sys_access (path, mode)
  2840.      char *path;
  2841.      int mode;
  2842. {
  2843.   static char *user = NULL;
  2844.   char dir_fn[512];
  2845.  
  2846.   /* translate possible directory spec into .DIR file name, so brain-dead
  2847.    * access() can treat the directory like a file.  */
  2848.   if (directory_file_name (path, dir_fn))
  2849.     path = dir_fn;
  2850.   
  2851.   if (mode == F_OK)
  2852.     return access (path, mode);
  2853.   if (user == NULL && (user = getenv ("USER")) == NULL)
  2854.     return -1;
  2855.   {
  2856.     int stat;
  2857.     int flags;
  2858.     int acces;
  2859.     int dummy;
  2860.     item itemlst[3];
  2861.     DESCRIPTOR(path_desc, path);
  2862.     DESCRIPTOR(user_desc, user);
  2863.  
  2864.     flags = 0;
  2865.     acces = 0;
  2866.     if ((mode & X_OK) && ((stat = access(path, mode)) < 0 || mode == X_OK))
  2867.         return stat;
  2868.     if (mode & R_OK)
  2869.         acces |= CHP$M_READ;
  2870.     if (mode & W_OK)
  2871.         acces |= CHP$M_WRITE;
  2872.     itemlst[0].buflen = sizeof (int);
  2873.     itemlst[0].code = CHP$_FLAGS;
  2874.     itemlst[0].bufadr = &flags;
  2875.     itemlst[0].retlenadr = &dummy;
  2876.     itemlst[1].buflen = sizeof (int);
  2877.     itemlst[1].code = CHP$_ACCESS;
  2878.     itemlst[1].bufadr = &acces;
  2879.     itemlst[1].retlenadr = &dummy;
  2880.     itemlst[2].end = CHP$_END;
  2881.     stat = SYS$CHECK_ACCESS(&ACL$C_FILE, &path_desc, &user_desc, itemlst);
  2882.     return stat == SS$_NORMAL ? 0 : -1;
  2883.     }
  2884. }
  2885.  
  2886. #else /* not VMS4_4 */
  2887.  
  2888. #include <prvdef.h>
  2889. #define    ACE$M_WRITE    2
  2890. #define    ACE$C_KEYID    1
  2891.  
  2892. static unsigned short memid, grpid;
  2893. static unsigned int uic;
  2894.  
  2895. /* Called from init_sys_modes, so it happens not very often
  2896.    but at least each time Emacs is loaded.  */
  2897. sys_access_reinit ()
  2898. {
  2899.   uic = 0;
  2900. }
  2901.  
  2902. int
  2903. sys_access (filename, type)
  2904.      char * filename;
  2905.      int type;
  2906. {
  2907.   struct FAB fab;
  2908.   struct XABPRO xab;
  2909.   int status, prvmask[2], size, i, typecode, acl_controlled;
  2910.   unsigned int *aclptr, *aclend, aclbuf[60];
  2911.  
  2912.   /* Get UIC and GRP values for protection checking.  */
  2913.   if (uic == 0)
  2914.     {
  2915.       status = LIB$GETJPI (&JPI$_UIC, 0, 0, &uic, 0, 0);
  2916.       if (! (status & 1))
  2917.     return -1;
  2918.       memid = uic & 0xFFFF;
  2919.       grpid = uic >> 16;
  2920.     }
  2921.  
  2922.   if (type != 2)        /* not checking write access */
  2923.     return access (filename, type);
  2924.  
  2925.   /* Check write protection. */
  2926.     
  2927. #define    CHECKPRIV(bit)    (prvmask[bit / 32] & (1 << (bit % 32)))
  2928. #define    WRITEABLE(field)  (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
  2929.  
  2930.   /* Find privilege bits */
  2931.   status = sys$setprv (0, 0, 0, prvmask);
  2932.   if (! (status & 1))
  2933.     error ("Unable to find privileges: %s", vmserrstr (status));
  2934.   if (CHECKPRIV (PRV$V_BYPASS))
  2935.     return 0;            /* BYPASS enabled */
  2936.   fab = cc$rms_fab;
  2937.   fab.fab$b_fac = FAB$M_GET;
  2938.   fab.fab$l_fna = filename;
  2939.   fab.fab$b_fns = strlen (filename);
  2940.   fab.fab$l_xab = &xab;
  2941.   xab = cc$rms_xabpro;
  2942.   xab.xab$l_aclbuf = aclbuf;
  2943.   xab.xab$w_aclsiz = sizeof (aclbuf);
  2944.   status = sys$open (&fab, 0, 0);
  2945.   if (! (status & 1))
  2946.     return -1;
  2947.   sys$close (&fab, 0, 0);
  2948.   /* Check system access */
  2949.   if (CHECKPRIV (PRV$V_SYSPRV) && WRITEABLE (XAB$V_SYS))
  2950.     return 0;
  2951.   /* Check ACL entries, if any */
  2952.   acl_controlled = 0;
  2953.   if (xab.xab$w_acllen > 0)
  2954.     {
  2955.       aclptr = aclbuf;
  2956.       aclend = &aclbuf[xab.xab$w_acllen / 4];
  2957.       while (*aclptr && aclptr < aclend)
  2958.     {
  2959.       size = (*aclptr & 0xff) / 4;
  2960.       typecode = (*aclptr >> 8) & 0xff;
  2961.       if (typecode == ACE$C_KEYID)
  2962.         for (i = size - 1; i > 1; i--)
  2963.           if (aclptr[i] == uic)
  2964.         {
  2965.           acl_controlled = 1;
  2966.           if (aclptr[1] & ACE$M_WRITE)
  2967.             return 0;    /* Write access through ACL */
  2968.         }
  2969.       aclptr = &aclptr[size];
  2970.     }
  2971.       if (acl_controlled)    /* ACL specified, prohibits write access */
  2972.     return -1;
  2973.     }
  2974.   /* No ACL entries specified, check normal protection */
  2975.   if (WRITEABLE (XAB$V_WLD))    /* World writeable */
  2976.     return 0;
  2977.   if (WRITEABLE (XAB$V_GRP) &&
  2978.       (unsigned short) (xab.xab$l_uic >> 16) == grpid)
  2979.     return 0;            /* Group writeable */
  2980.   if (WRITEABLE (XAB$V_OWN) &&
  2981.       (xab.xab$l_uic & 0xFFFF) == memid)
  2982.     return 0;            /* Owner writeable */
  2983.  
  2984.   return -1;    /* Not writeable */
  2985. }
  2986. #endif /* not VMS4_4 */
  2987. #endif /* access */
  2988.   
  2989. static char vtbuf[NAM$C_MAXRSS+1];
  2990.  
  2991. /* translate a vms file spec to a unix path */
  2992. char *
  2993. sys_translate_vms (vfile)
  2994.      char * vfile;
  2995. {
  2996.   char * p;
  2997.   char * targ;
  2998.  
  2999.   if (!vfile)
  3000.     return 0;
  3001.  
  3002.   targ = vtbuf;
  3003.  
  3004.   /* leading device or logical name is a root directory */
  3005.   if (p = strchr (vfile, ':'))
  3006.     {
  3007.       *targ++ = '/';
  3008.       while (vfile < p)
  3009.     *targ++ = *vfile++;
  3010.       vfile++;
  3011.       *targ++ = '/';
  3012.     }
  3013.   p = vfile;
  3014.   if (*p == '[' || *p == '<')
  3015.     {
  3016.       while (*++vfile != *p + 2)
  3017.     switch (*vfile)
  3018.       {
  3019.       case '.':
  3020.         if (vfile[-1] == *p)
  3021.           *targ++ = '.';
  3022.         *targ++ = '/';
  3023.         break;
  3024.  
  3025.       case '-':
  3026.         *targ++ = '.';
  3027.         *targ++ = '.';
  3028.         break;
  3029.         
  3030.       default:
  3031.         *targ++ = *vfile;
  3032.         break;
  3033.       }
  3034.       vfile++;
  3035.       *targ++ = '/';
  3036.     }
  3037.   while (*vfile)
  3038.     *targ++ = *vfile++;
  3039.  
  3040.   return vtbuf;
  3041. }
  3042.  
  3043. static char utbuf[NAM$C_MAXRSS+1];
  3044.  
  3045. /* translate a unix path to a VMS file spec */
  3046. char *
  3047. sys_translate_unix (ufile)
  3048.      char * ufile;
  3049. {
  3050.   int slash_seen = 0;
  3051.   char *p;
  3052.   char * targ;
  3053.  
  3054.   if (!ufile)
  3055.     return 0;
  3056.  
  3057.   targ = utbuf;
  3058.  
  3059.   if (*ufile == '/')
  3060.     {
  3061.       ufile++;
  3062.     }
  3063.  
  3064.   while (*ufile)
  3065.     {
  3066.       switch (*ufile)
  3067.     {
  3068.     case '/':
  3069.       if (slash_seen)
  3070.         if (index (&ufile[1], '/'))
  3071.           *targ++ = '.';
  3072.         else
  3073.           *targ++ = ']';
  3074.       else
  3075.         {
  3076.           *targ++ = ':';
  3077.           if (index (&ufile[1], '/'))
  3078.         *targ++ = '[';
  3079.           slash_seen = 1;
  3080.         }
  3081.       break;
  3082.  
  3083.     case '.':
  3084.       if (strncmp (ufile, "./", 2) == 0)
  3085.         {
  3086.           if (!slash_seen)
  3087.         {
  3088.           *targ++ = '[';
  3089.           slash_seen = 1;
  3090.         }
  3091.           ufile++;        /* skip the dot */
  3092.           if (index (&ufile[1], '/'))
  3093.         *targ++ = '.';
  3094.           else
  3095.         *targ++ = ']';
  3096.         }
  3097.       else if (strncmp (ufile, "../", 3) == 0)
  3098.         {
  3099.           if (!slash_seen)
  3100.         {
  3101.           *targ++ = '[';
  3102.           slash_seen = 1;
  3103.         }
  3104.           *targ++ = '-';
  3105.           ufile += 2;    /* skip the dots */
  3106.           if (index (&ufile[1], '/'))
  3107.         *targ++ = '.';
  3108.           else
  3109.         *targ++ = ']';
  3110.         }
  3111.       else
  3112.         *targ++ = *ufile;
  3113.       break;
  3114.  
  3115.     default:
  3116.       *targ++ = *ufile;
  3117.       break;
  3118.     }
  3119.       ufile++;
  3120.     }
  3121.   *targ = '\0';
  3122.   
  3123.   return utbuf;
  3124. }
  3125.  
  3126. char *
  3127. getwd (pathname)
  3128.      char *pathname;
  3129. {
  3130.   char *ptr;
  3131.   strcpy (pathname, egetenv ("PATH"));
  3132.  
  3133.   ptr = pathname;
  3134.   while (*ptr)
  3135.     {
  3136.       if ('a' <= *ptr && *ptr <= 'z')
  3137.     *ptr -= 040;
  3138.       ptr++;
  3139.     }
  3140.   return pathname;
  3141. }
  3142.  
  3143. getppid ()
  3144. {
  3145.   long item_code = JPI$_OWNER;
  3146.   unsigned long parent_id;
  3147.   int status;
  3148.  
  3149.   if (((status = LIB$GETJPI (&item_code, 0, 0, &parent_id)) & 1) == 0)
  3150.     {
  3151.       errno = EVMSERR;
  3152.       vaxc$errno = status;
  3153.       return -1;
  3154.     }
  3155.   return parent_id;
  3156. }
  3157.  
  3158. #undef getuid
  3159. unsigned
  3160. sys_getuid ()
  3161. {
  3162.   return (getgid () << 16) | getuid ();
  3163. }
  3164.  
  3165. int
  3166. sys_read (fildes, buf, nbyte)
  3167.      int fildes;
  3168.      char *buf;
  3169.      unsigned int nbyte;
  3170. {
  3171.   return read (fildes, buf, (nbyte < MAXIOSIZE ? nbyte : MAXIOSIZE));
  3172. }
  3173.  
  3174. #if 0
  3175. int
  3176. sys_write (fildes, buf, nbyte)
  3177.      int fildes;
  3178.      char *buf;
  3179.      unsigned int nbyte;
  3180. {
  3181.   register int nwrote, rtnval = 0;
  3182.  
  3183.   while (nbyte > MAXIOSIZE && (nwrote = write (fildes, buf, MAXIOSIZE)) > 0) {
  3184.     nbyte -= nwrote;
  3185.     buf += nwrote;
  3186.     rtnval += nwrote;
  3187.   }
  3188.   if (nwrote < 0)
  3189.     return rtnval ? rtnval : -1;
  3190.   if ((nwrote = write (fildes, buf, nbyte)) < 0)
  3191.     return rtnval ? rtnval : -1;
  3192.   return (rtnval + nwrote);
  3193. }
  3194. #endif /* 0 */
  3195.  
  3196. /*
  3197.  *    VAX/VMS VAX C RTL really loses. It insists that records
  3198.  *      end with a newline (carriage return) character, and if they
  3199.  *    don't it adds one (nice of it isn't it!)
  3200.  *
  3201.  *    Thus we do this stupidity below.
  3202.  */
  3203.  
  3204. int
  3205. sys_write (fildes, buf, nbytes)
  3206.      int fildes;
  3207.      char *buf;
  3208.      unsigned int nbytes;
  3209. {
  3210.   register char *p;
  3211.   register char *e;
  3212.   int retval, sum;
  3213.   p = buf;
  3214.   sum = 0;
  3215.   while (nbytes > 0)
  3216.     {
  3217.       e =  p + min (MAXIOSIZE, nbytes) - 1;
  3218.       while (*e != '\n' && e > p) e--;
  3219.       if (p == e)        /* Ok.. so here we add a newline... sigh. */
  3220.     e = p + min (MAXIOSIZE, nbytes) - 1;
  3221.       retval = write (fildes, p, e - p + 1);
  3222.       if (retval != e - p + 1) return -1;
  3223.       p = e + 1;
  3224.       sum = sum + retval;
  3225.       nbytes -= retval;
  3226.     }
  3227.   return sum;
  3228. }
  3229.  
  3230. /* Create file NEW copying its attributes from file OLD.  If
  3231.    OLD is 0 or does not exist, create based on the value of
  3232.    vms_stmlf_recfm. */
  3233.  
  3234. int
  3235. creat_copy_attrs (old, new)
  3236.      char *old, *new;
  3237. {
  3238.   struct FAB fab = cc$rms_fab;
  3239.   struct XABPRO xabpro;
  3240.   char aclbuf[256];    /* Choice of size is arbitrary.  See below. */
  3241.   extern int vms_stmlf_recfm;
  3242.  
  3243.   if (old)
  3244.     {
  3245.       fab.fab$b_fac = FAB$M_GET;
  3246.       fab.fab$l_fna = old;
  3247.       fab.fab$b_fns = strlen (old);
  3248.       fab.fab$l_xab = &xabpro;
  3249.       xabpro = cc$rms_xabpro;
  3250.       xabpro.xab$l_aclbuf = aclbuf;
  3251.       xabpro.xab$w_aclsiz = sizeof aclbuf;
  3252.       /* Call $OPEN to fill in the fab & xabpro fields. */
  3253.       if (sys$open (&fab, 0, 0) & 1)
  3254.     {
  3255.       sys$close (&fab, 0, 0);
  3256.       fab.fab$l_alq = 0;    /* zero the allocation quantity */
  3257.       if (xabpro.xab$w_acllen > 0)
  3258.         {
  3259.           if (xabpro.xab$w_acllen > sizeof aclbuf)
  3260.         /* If the acl buffer was too short, redo open with longer one.
  3261.            Wouldn't need to do this if there were some system imposed
  3262.            limit on the size of an ACL, but I can't find any such. */
  3263.         {
  3264.           xabpro.xab$l_aclbuf = alloca (xabpro.xab$w_acllen);
  3265.           xabpro.xab$w_aclsiz = xabpro.xab$w_acllen;
  3266.           if (sys$open (&fab, 0, 0) & 1)
  3267.             sys$close (&fab, 0, 0);
  3268.           else
  3269.             old = 0;
  3270.         }
  3271.         }
  3272.       else
  3273.         xabpro.xab$l_aclbuf = 0;
  3274.     }
  3275.       else
  3276.     old = 0;
  3277.     }
  3278.   fab.fab$l_fna = new;
  3279.   fab.fab$b_fns = strlen (new);
  3280.   if (!old)
  3281.     {
  3282.       fab.fab$l_xab = 0;
  3283.       fab.fab$b_rfm = vms_stmlf_recfm ? FAB$C_STMLF : FAB$C_VAR;
  3284.       fab.fab$b_rat = FAB$M_CR;
  3285.     }
  3286.   /* Create the new file with either default attrs or attrs copied
  3287.      from old file. */
  3288.   if (!(SYS$CREATE (&fab, 0, 0) & 1))
  3289.     return -1;
  3290.   sys$close (&fab, 0, 0);
  3291.   /* As this is a "replacement" for creat, return a file descriptor
  3292.      opened for writing. */
  3293.   return open (new, O_WRONLY);
  3294. }
  3295.  
  3296. #ifdef creat
  3297. #undef creat
  3298. #include <varargs.h>
  3299.  
  3300. sys_creat (va_alist)
  3301.      va_dcl
  3302. {
  3303.   va_list list_incrementor;
  3304.   char *name;
  3305.   int mode;
  3306.   int rfd;            /* related file descriptor */
  3307.   int fd;            /* Our new file descriptor */
  3308.   int count;
  3309.   struct stat st_buf;
  3310.   char rfm[12];
  3311.   char rat[15];
  3312.   char mrs[13];
  3313.   char fsz[13];
  3314.   extern int vms_stmlf_recfm;
  3315.  
  3316.   va_count (count);
  3317.   va_start (list_incrementor);
  3318.   name = va_arg (list_incrementor, char *);
  3319.   mode = va_arg (list_incrementor, int);
  3320.   if (count > 2)
  3321.     rfd = va_arg (list_incrementor, int);
  3322.   va_end (list_incrementor);
  3323.   if (count > 2)
  3324.     {
  3325.       /* Use information from the related file descriptor to set record
  3326.      format of the newly created file. */
  3327.       fstat (rfd, &st_buf);
  3328.       switch (st_buf.st_fab_rfm)
  3329.     {
  3330.     case FAB$C_FIX:
  3331.       strcpy (rfm, "rfm = fix");
  3332.       sprintf (mrs, "mrs = %d", st_buf.st_fab_mrs);
  3333.       strcpy (rat, "rat = ");
  3334.       if (st_buf.st_fab_rat & FAB$M_CR)
  3335.         strcat (rat, "cr");
  3336.       else if (st_buf.st_fab_rat & FAB$M_FTN)
  3337.         strcat (rat, "ftn");
  3338.       else if (st_buf.st_fab_rat & FAB$M_PRN)
  3339.         strcat (rat, "prn");
  3340.       if (st_buf.st_fab_rat & FAB$M_BLK)
  3341.         if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
  3342.           strcat (rat, ", blk");
  3343.         else
  3344.           strcat (rat, "blk");
  3345.       return creat (name, 0, rfm, rat, mrs);
  3346.  
  3347.     case FAB$C_VFC:
  3348.       strcpy (rfm, "rfm = vfc");
  3349.       sprintf (fsz, "fsz = %d", st_buf.st_fab_fsz);
  3350.       strcpy (rat, "rat = ");
  3351.       if (st_buf.st_fab_rat & FAB$M_CR)
  3352.         strcat (rat, "cr");
  3353.       else if (st_buf.st_fab_rat & FAB$M_FTN)
  3354.         strcat (rat, "ftn");
  3355.       else if (st_buf.st_fab_rat & FAB$M_PRN)
  3356.         strcat (rat, "prn");
  3357.       if (st_buf.st_fab_rat & FAB$M_BLK)
  3358.         if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
  3359.           strcat (rat, ", blk");
  3360.         else
  3361.           strcat (rat, "blk");
  3362.       return creat (name, 0, rfm, rat, fsz);
  3363.  
  3364.     case FAB$C_STM:
  3365.       strcpy (rfm, "rfm = stm");
  3366.       break;
  3367.  
  3368.     case FAB$C_STMCR:
  3369.       strcpy (rfm, "rfm = stmcr");
  3370.       break;
  3371.  
  3372.     case FAB$C_STMLF:
  3373.       strcpy (rfm, "rfm = stmlf");
  3374.       break;
  3375.  
  3376.     case FAB$C_UDF:
  3377.       strcpy (rfm, "rfm = udf");
  3378.       break;
  3379.  
  3380.     case FAB$C_VAR:
  3381.       strcpy (rfm, "rfm = var");
  3382.       break;
  3383.     }
  3384.       strcpy (rat, "rat = ");
  3385.       if (st_buf.st_fab_rat & FAB$M_CR)
  3386.     strcat (rat, "cr");
  3387.       else if (st_buf.st_fab_rat & FAB$M_FTN)
  3388.     strcat (rat, "ftn");
  3389.       else if (st_buf.st_fab_rat & FAB$M_PRN)
  3390.     strcat (rat, "prn");
  3391.       if (st_buf.st_fab_rat & FAB$M_BLK)
  3392.     if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
  3393.       strcat (rat, ", blk");
  3394.     else
  3395.       strcat (rat, "blk");
  3396.     }
  3397.   else
  3398.     {
  3399.       strcpy (rfm, vms_stmlf_recfm ? "rfm = stmlf" : "rfm=var");
  3400.       strcpy (rat, "rat=cr");
  3401.     }
  3402.   /* Until the VAX C RTL fixes the many bugs with modes, always use
  3403.      mode 0 to get the user's default protection. */
  3404.   fd = creat (name, 0, rfm, rat);
  3405.   if (fd < 0 && errno == EEXIST)
  3406.     {
  3407.       if (unlink (name) < 0)
  3408.     report_file_error ("delete", build_string (name));
  3409.       fd = creat (name, 0, rfm, rat);
  3410.     }
  3411.   return fd;
  3412. }
  3413. #endif /* creat */
  3414.  
  3415. /* fwrite to stdout is S L O W.  Speed it up by using fputc...*/
  3416. sys_fwrite (ptr, size, num, fp)
  3417.      register char * ptr;
  3418.      FILE * fp;
  3419. {
  3420.   register int tot = num * size;
  3421.  
  3422.   while (tot--)
  3423.     fputc (*ptr++, fp);
  3424. }
  3425.  
  3426. /*
  3427.  * The VMS C library routine creat() actually creates a new version of an
  3428.  * existing file rather than truncating the old version.  There are times
  3429.  * when this is not the desired behavior, for instance, when writing an
  3430.  * auto save file (you only want one version), or when you don't have
  3431.  * write permission in the directory containing the file (but the file
  3432.  * itself is writable).  Hence this routine, which is equivalent to 
  3433.  * "close (creat (fn, 0));" on Unix if fn already exists.
  3434.  */
  3435. int
  3436. vms_truncate (fn)
  3437.      char *fn;
  3438. {
  3439.   struct FAB xfab = cc$rms_fab;
  3440.   struct RAB xrab = cc$rms_rab;
  3441.   int status;
  3442.  
  3443.   xfab.fab$l_fop = FAB$M_TEF;    /* free allocated but unused blocks on close */
  3444.   xfab.fab$b_fac = FAB$M_TRN | FAB$M_GET; /* allow truncate and get access */
  3445.   xfab.fab$b_shr = FAB$M_NIL;    /* allow no sharing - file must be locked */
  3446.   xfab.fab$l_fna = fn;
  3447.   xfab.fab$b_fns = strlen (fn);
  3448.   xfab.fab$l_dna = ";0";    /* default to latest version of the file */
  3449.   xfab.fab$b_dns = 2;
  3450.   xrab.rab$l_fab = &xfab;
  3451.  
  3452.   /* This gibberish opens the file, positions to the first record, and
  3453.      deletes all records from there until the end of file. */
  3454.   if ((sys$open (&xfab) & 01) == 01)
  3455.     {
  3456.       if ((sys$connect (&xrab) & 01) == 01 &&
  3457.       (sys$find (&xrab) & 01) == 01 &&
  3458.       (sys$truncate (&xrab) & 01) == 01)
  3459.     status = 0;
  3460.       else
  3461.     status = -1;
  3462.     }
  3463.   else
  3464.     status = -1;
  3465.   sys$close (&xfab);
  3466.   return status;
  3467. }
  3468.  
  3469. /* Define this symbol to actually read SYSUAF.DAT.  This requires either
  3470.    SYSPRV or a readable SYSUAF.DAT. */
  3471.  
  3472. #ifdef READ_SYSUAF
  3473. /*
  3474.  * getuaf.c
  3475.  *
  3476.  * Routine to read the VMS User Authorization File and return
  3477.  * a specific user's record.
  3478.  */
  3479.  
  3480. static struct UAF retuaf;
  3481.  
  3482. struct UAF *
  3483. get_uaf_name(uname)
  3484.      char * uname;
  3485. {
  3486.   register status;
  3487.   struct FAB uaf_fab;
  3488.   struct RAB uaf_rab;
  3489.   
  3490.   uaf_fab = cc$rms_fab;
  3491.   uaf_rab = cc$rms_rab;
  3492.   /* initialize fab fields */
  3493.   uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
  3494.   uaf_fab.fab$b_fns = 21;
  3495.   uaf_fab.fab$b_fac = FAB$M_GET;
  3496.   uaf_fab.fab$b_org = FAB$C_IDX;
  3497.   uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
  3498.   /* initialize rab fields */
  3499.   uaf_rab.rab$l_fab = &uaf_fab;
  3500.   /* open the User Authorization File */
  3501.   status = sys$open(&uaf_fab);
  3502.   if (!(status&1))
  3503.     {
  3504.       errno = EVMSERR;
  3505.       vaxc$errno = status;
  3506.       return 0;
  3507.     }
  3508.   status = sys$connect(&uaf_rab);
  3509.   if (!(status&1))
  3510.     {
  3511.       errno = EVMSERR;
  3512.       vaxc$errno = status;
  3513.       return 0;
  3514.     }
  3515.   /* read the requested record - index is in uname */
  3516.   uaf_rab.rab$l_kbf = uname;
  3517.   uaf_rab.rab$b_ksz = strlen (uname);
  3518.   uaf_rab.rab$b_rac = RAB$C_KEY;
  3519.   uaf_rab.rab$l_ubf = (char *)&retuaf;
  3520.   uaf_rab.rab$w_usz = sizeof retuaf;
  3521.   status = sys$get(&uaf_rab);
  3522.   if (!(status&1))
  3523.     {
  3524.       errno = EVMSERR;
  3525.       vaxc$errno = status;
  3526.       return 0;
  3527.     }
  3528.   /* close the User Authorization File */
  3529.   status = sys$disconnect(&uaf_rab);
  3530.   if (!(status&1))
  3531.     {
  3532.       errno = EVMSERR;
  3533.       vaxc$errno = status;
  3534.       return 0;
  3535.     }
  3536.   status = sys$close(&uaf_fab);
  3537.   if (!(status&1))
  3538.     {
  3539.       errno = EVMSERR;
  3540.       vaxc$errno = status;
  3541.       return 0;
  3542.     }
  3543.   return &retuaf;
  3544. }
  3545.  
  3546. struct UAF *
  3547. get_uaf_uic(uic)
  3548.      unsigned long uic;
  3549. {
  3550.   register status;
  3551.   struct FAB uaf_fab;
  3552.   struct RAB uaf_rab;
  3553.   
  3554.   uaf_fab = cc$rms_fab;
  3555.   uaf_rab = cc$rms_rab;
  3556.   /* initialize fab fields */
  3557.   uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
  3558.   uaf_fab.fab$b_fns = 21;
  3559.   uaf_fab.fab$b_fac = FAB$M_GET;
  3560.   uaf_fab.fab$b_org = FAB$C_IDX;
  3561.   uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
  3562.   /* initialize rab fields */
  3563.   uaf_rab.rab$l_fab = &uaf_fab;
  3564.   /* open the User Authorization File */
  3565.   status = sys$open(&uaf_fab);
  3566.   if (!(status&1))
  3567.     {
  3568.       errno = EVMSERR;
  3569.       vaxc$errno = status;
  3570.       return 0;
  3571.     }
  3572.   status = sys$connect(&uaf_rab);
  3573.   if (!(status&1))
  3574.     {
  3575.       errno = EVMSERR;
  3576.       vaxc$errno = status;
  3577.       return 0;
  3578.     }
  3579.   /* read the requested record - index is in uic */
  3580.   uaf_rab.rab$b_krf = 1;    /* 1st alternate key */
  3581.   uaf_rab.rab$l_kbf = (char *) &uic;
  3582.   uaf_rab.rab$b_ksz = sizeof uic;
  3583.   uaf_rab.rab$b_rac = RAB$C_KEY;
  3584.   uaf_rab.rab$l_ubf = (char *)&retuaf;
  3585.   uaf_rab.rab$w_usz = sizeof retuaf;
  3586.   status = sys$get(&uaf_rab);
  3587.   if (!(status&1))
  3588.     {
  3589.       errno = EVMSERR;
  3590.       vaxc$errno = status;
  3591.       return 0;
  3592.     }
  3593.   /* close the User Authorization File */
  3594.   status = sys$disconnect(&uaf_rab);
  3595.   if (!(status&1))
  3596.     {
  3597.       errno = EVMSERR;
  3598.       vaxc$errno = status;
  3599.       return 0;
  3600.     }
  3601.   status = sys$close(&uaf_fab);
  3602.   if (!(status&1))
  3603.     {
  3604.       errno = EVMSERR;
  3605.       vaxc$errno = status;
  3606.       return 0;
  3607.     }
  3608.   return &retuaf;
  3609. }
  3610.  
  3611. static struct passwd retpw;
  3612.  
  3613. struct passwd *
  3614. cnv_uaf_pw (up)
  3615.      struct UAF * up;
  3616. {
  3617.   char * ptr;
  3618.  
  3619.   /* copy these out first because if the username is 32 chars, the next
  3620.      section will overwrite the first byte of the UIC */
  3621.   retpw.pw_uid = up->uaf$w_mem;
  3622.   retpw.pw_gid = up->uaf$w_grp;
  3623.  
  3624.   /* I suppose this is not the best sytle, to possibly overwrite one
  3625.      byte beyond the end of the field, but what the heck... */
  3626.   ptr = &up->uaf$t_username[UAF$S_USERNAME];
  3627.   while (ptr[-1] == ' ')
  3628.     ptr--;
  3629.   *ptr = '\0';
  3630.   strcpy (retpw.pw_name, up->uaf$t_username);
  3631.  
  3632.   /* the rest of these are counted ascii strings */
  3633.   strncpy (retpw.pw_gecos, &up->uaf$t_owner[1], up->uaf$t_owner[0]);
  3634.   retpw.pw_gecos[up->uaf$t_owner[0]] = '\0';
  3635.   strncpy (retpw.pw_dir, &up->uaf$t_defdev[1], up->uaf$t_defdev[0]);
  3636.   retpw.pw_dir[up->uaf$t_defdev[0]] = '\0';
  3637.   strncat (retpw.pw_dir, &up->uaf$t_defdir[1], up->uaf$t_defdir[0]);
  3638.   retpw.pw_dir[up->uaf$t_defdev[0] + up->uaf$t_defdir[0]] = '\0';
  3639.   strncpy (retpw.pw_shell, &up->uaf$t_defcli[1], up->uaf$t_defcli[0]);
  3640.   retpw.pw_shell[up->uaf$t_defcli[0]] = '\0';
  3641.  
  3642.   return &retpw;
  3643. }
  3644. #else /* not READ_SYSUAF */
  3645. static struct passwd retpw;
  3646. #endif /* not READ_SYSUAF */
  3647.  
  3648. struct passwd *
  3649. getpwnam (name)
  3650.      char * name;
  3651. {
  3652. #ifdef READ_SYSUAF
  3653.   struct UAF *up;
  3654. #else
  3655.   char * user;
  3656.   char * dir;
  3657.   char * full;
  3658. #endif /* READ_SYSUAF */
  3659.   char *ptr = name;
  3660.  
  3661.   while (*ptr)
  3662.     {
  3663.       if ('a' <= *ptr && *ptr <= 'z')
  3664.     *ptr -= 040;
  3665.       ptr++;
  3666.     }
  3667. #ifdef READ_SYSUAF
  3668.   if (!(up = get_uaf_name (name)))
  3669.     return 0;
  3670.   return cnv_uaf_pw (up);
  3671. #else
  3672.   if (strcmp (name, getenv ("USER")) == 0)
  3673.     {
  3674.       retpw.pw_uid = getuid ();
  3675.       retpw.pw_gid = getgid ();
  3676.       strcpy (retpw.pw_name, name);
  3677.       if (full = egetenv ("FULLNAME"))
  3678.     strcpy (retpw.pw_gecos, full);
  3679.       else
  3680.     *retpw.pw_gecos = '\0';
  3681.       strcpy (retpw.pw_dir, egetenv ("HOME"));
  3682.       *retpw.pw_shell = '\0';
  3683.       return &retpw;
  3684.     }
  3685.   else
  3686.     return 0;
  3687. #endif /* not READ_SYSUAF */
  3688. }
  3689.  
  3690. struct passwd *
  3691. getpwuid (uid)
  3692.      unsigned long uid;
  3693. {
  3694. #ifdef READ_SYSUAF
  3695.   struct UAF * up;
  3696.  
  3697.   if (!(up = get_uaf_uic (uid)))
  3698.     return 0;
  3699.   return cnv_uaf_pw (up);
  3700. #else
  3701.   if (uid == sys_getuid ())
  3702.     return getpwnam (egetenv ("USER"));
  3703.   else
  3704.     return 0;
  3705. #endif /* not READ_SYSUAF */
  3706. }
  3707.  
  3708. /* return total address space available to the current process.  This is
  3709.    the sum of the current p0 size, p1 size and free page table entries
  3710.    available. */
  3711. vlimit ()
  3712. {
  3713.   int item_code;
  3714.   unsigned long free_pages;
  3715.   unsigned long frep0va;
  3716.   unsigned long frep1va;
  3717.   register status;
  3718.  
  3719.   item_code = JPI$_FREPTECNT;
  3720.   if (((status = LIB$GETJPI (&item_code, 0, 0, &free_pages)) & 1) == 0)
  3721.     {
  3722.       errno = EVMSERR;
  3723.       vaxc$errno = status;
  3724.       return -1;
  3725.     }
  3726.   free_pages *= 512;
  3727.  
  3728.   item_code = JPI$_FREP0VA;
  3729.   if (((status = LIB$GETJPI (&item_code, 0, 0, &frep0va)) & 1) == 0)
  3730.     {
  3731.       errno = EVMSERR;
  3732.       vaxc$errno = status;
  3733.       return -1;
  3734.     }
  3735.   item_code = JPI$_FREP1VA;
  3736.   if (((status = LIB$GETJPI (&item_code, 0, 0, &frep1va)) & 1) == 0)
  3737.     {
  3738.       errno = EVMSERR;
  3739.       vaxc$errno = status;
  3740.       return -1;
  3741.     }
  3742.  
  3743.   return free_pages + frep0va + (0x7fffffff - frep1va);
  3744. }
  3745.  
  3746. define_logical_name (varname, string)
  3747.      char *varname;
  3748.      char *string;
  3749. {
  3750.   struct dsc$descriptor_s strdsc =
  3751.     {strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string};
  3752.   struct dsc$descriptor_s envdsc =
  3753.     {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
  3754.   struct dsc$descriptor_s lnmdsc =
  3755.     {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
  3756.  
  3757.   return LIB$SET_LOGICAL (&envdsc, &strdsc, &lnmdsc, 0, 0);
  3758. }
  3759.  
  3760. delete_logical_name (varname)
  3761.      char *varname;
  3762. {
  3763.   struct dsc$descriptor_s envdsc =
  3764.     {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
  3765.   struct dsc$descriptor_s lnmdsc =
  3766.     {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
  3767.  
  3768.   return LIB$DELETE_LOGICAL (&envdsc, &lnmdsc);
  3769. }
  3770.  
  3771. ulimit()
  3772. {}
  3773.  
  3774. setpriority()
  3775. {}
  3776.  
  3777. setpgrp()
  3778. {}
  3779.  
  3780. execvp()
  3781. {
  3782.   error ("execvp system call not implemented");
  3783. }
  3784.  
  3785. int
  3786. rename (from, to)
  3787.      char *from, *to;
  3788. {
  3789.   int status;
  3790.   struct FAB from_fab = cc$rms_fab, to_fab = cc$rms_fab;
  3791.   struct NAM from_nam = cc$rms_nam, to_nam = cc$rms_nam;
  3792.   char from_esn[NAM$C_MAXRSS];
  3793.   char to_esn[NAM$C_MAXRSS];
  3794.  
  3795.   from_fab.fab$l_fna = from;
  3796.   from_fab.fab$b_fns = strlen (from);
  3797.   from_fab.fab$l_nam = &from_nam;
  3798.   from_fab.fab$l_fop = FAB$M_NAM;
  3799.  
  3800.   from_nam.nam$l_esa = from_esn;
  3801.   from_nam.nam$b_ess = sizeof from_esn;
  3802.  
  3803.   to_fab.fab$l_fna = to;
  3804.   to_fab.fab$b_fns = strlen (to);
  3805.   to_fab.fab$l_nam = &to_nam;
  3806.   to_fab.fab$l_fop = FAB$M_NAM;
  3807.  
  3808.   to_nam.nam$l_esa = to_esn;
  3809.   to_nam.nam$b_ess = sizeof to_esn;
  3810.  
  3811.   status = SYS$RENAME (&from_fab, 0, 0, &to_fab);
  3812.  
  3813.   if (status & 1)
  3814.     return 0;
  3815.   else
  3816.     {
  3817.       if (status == RMS$_DEV)
  3818.     errno = EXDEV;
  3819.       else
  3820.     errno = EVMSERR;
  3821.       vaxc$errno = status;
  3822.       return -1;
  3823.     }
  3824. }
  3825.  
  3826. link (file, new)
  3827.      char * file, * new;
  3828. {
  3829.   register status;
  3830.   struct FAB fab;
  3831.   struct NAM nam;
  3832.   unsigned short fid[3];
  3833.   char esa[NAM$C_MAXRSS];
  3834.  
  3835.   fab = cc$rms_fab;
  3836.   fab.fab$l_fop = FAB$M_OFP;
  3837.   fab.fab$l_fna = file;
  3838.   fab.fab$b_fns = strlen (file);
  3839.   fab.fab$l_nam = &nam;
  3840.  
  3841.   nam = cc$rms_nam;
  3842.   nam.nam$l_esa = esa;
  3843.   nam.nam$b_ess = NAM$C_MAXRSS;
  3844.  
  3845.   status = SYS$PARSE (&fab);
  3846.   if ((status & 1) == 0)
  3847.     {
  3848.       errno = EVMSERR;
  3849.       vaxc$errno = status;
  3850.       return -1;
  3851.     }
  3852.   status = SYS$SEARCH (&fab);
  3853.   if ((status & 1) == 0)
  3854.     {
  3855.       errno = EVMSERR;
  3856.       vaxc$errno = status;
  3857.       return -1;
  3858.     }
  3859.  
  3860.   fid[0] = nam.nam$w_fid[0];
  3861.   fid[1] = nam.nam$w_fid[1];
  3862.   fid[2] = nam.nam$w_fid[2];
  3863.  
  3864.   fab.fab$l_fna = new;
  3865.   fab.fab$b_fns = strlen (new);
  3866.  
  3867.   status = SYS$PARSE (&fab);
  3868.   if ((status & 1) == 0)
  3869.     {
  3870.       errno = EVMSERR;
  3871.       vaxc$errno = status;
  3872.       return -1;
  3873.     }
  3874.  
  3875.   nam.nam$w_fid[0] = fid[0];
  3876.   nam.nam$w_fid[1] = fid[1];
  3877.   nam.nam$w_fid[2] = fid[2];
  3878.  
  3879.   nam.nam$l_esa = nam.nam$l_name;
  3880.   nam.nam$b_esl = nam.nam$b_name + nam.nam$b_type + nam.nam$b_ver;
  3881.  
  3882.   status = SYS$ENTER (&fab);
  3883.   if ((status & 1) == 0)
  3884.     {
  3885.       errno = EVMSERR;
  3886.       vaxc$errno = status;
  3887.       return -1;
  3888.     }
  3889.  
  3890.   return 0;
  3891. }
  3892.  
  3893. croak (badfunc)
  3894.      char *badfunc;
  3895. {
  3896.   printf ("%s not yet implemented\r\n", badfunc);
  3897.   reset_sys_modes ();
  3898.   exit (1);
  3899. }
  3900.  
  3901. long
  3902. random ()
  3903. {
  3904.   /* Arrange to return a range centered on zero.  */
  3905.   return rand () - (1 << 30);
  3906. }
  3907.  
  3908. srandom (seed)
  3909. {
  3910.   srand (seed);
  3911. }
  3912. #endif /* VMS */
  3913.  
  3914. #ifdef AIX
  3915.  
  3916. /* Called from init_sys_modes.  */
  3917. hft_init ()
  3918. {
  3919.   /* If we're not on an HFT we shouldn't do any of this.  We determine
  3920.      if we are on an HFT by trying to get an HFT error code.  If this
  3921.      call fails, we're not on an HFT. */ 
  3922. #ifdef IBMR2AIX
  3923.   if (ioctl (0, HFQERROR, 0) < 0)
  3924.     return;
  3925. #else /* not IBMR2AIX */
  3926.   if (ioctl (0, HFQEIO, 0) < 0)
  3927.     return;
  3928. #endif /* not IBMR2AIX */
  3929.  
  3930.   /* On AIX the default hft keyboard mapping uses backspace rather than delete
  3931.      as the rubout key's ASCII code.  Here this is changed.  The bug is that
  3932.      there's no way to determine the old mapping, so in reset_sys_modes
  3933.      we need to assume that the normal map had been present.  Of course, this
  3934.      code also doesn't help if on a terminal emulator which doesn't understand
  3935.      HFT VTD's. */
  3936.   {
  3937.     struct hfbuf buf;
  3938.     struct hfkeymap keymap;
  3939.  
  3940.     buf.hf_bufp = (char *)&keymap;
  3941.     buf.hf_buflen = sizeof (keymap);
  3942.     keymap.hf_nkeys = 2;
  3943.     keymap.hfkey[0].hf_kpos = 15;
  3944.     keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
  3945. #ifdef IBMR2AIX
  3946.     keymap.hfkey[0].hf_keyidh = '<';
  3947. #else /* not IBMR2AIX */
  3948.     keymap.hfkey[0].hf_page = '<';
  3949. #endif /* not IBMR2AIX */
  3950.     keymap.hfkey[0].hf_char = 127;
  3951.     keymap.hfkey[1].hf_kpos = 15;
  3952.     keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
  3953. #ifdef IBMR2AIX
  3954.     keymap.hfkey[1].hf_keyidh = '<';
  3955. #else /* not IBMR2AIX */
  3956.     keymap.hfkey[1].hf_page = '<';
  3957. #endif /* not IBMR2AIX */
  3958.     keymap.hfkey[1].hf_char = 127;
  3959.     hftctl (0, HFSKBD, &buf);
  3960.   }
  3961.   /* The HFT system on AIX doesn't optimize for scrolling, so it's really ugly
  3962.      at times. */
  3963.   line_ins_del_ok = char_ins_del_ok = 0;
  3964. }
  3965.  
  3966. /* Reset the rubout key to backspace. */
  3967.  
  3968. hft_reset ()
  3969. {
  3970.   struct hfbuf buf;
  3971.   struct hfkeymap keymap;
  3972.  
  3973. #ifdef IBMR2AIX
  3974.   if (ioctl (0, HFQERROR, 0) < 0)
  3975.     return;
  3976. #else /* not IBMR2AIX */
  3977.   if (ioctl (0, HFQEIO, 0) < 0)
  3978.     return;
  3979. #endif /* not IBMR2AIX */
  3980.  
  3981.   buf.hf_bufp = (char *)&keymap;
  3982.   buf.hf_buflen = sizeof(keymap);
  3983.   keymap.hf_nkeys = 2;
  3984.   keymap.hfkey[0].hf_kpos = 15;
  3985.   keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
  3986. #ifdef IBMR2AIX
  3987.   keymap.hfkey[0].hf_keyidh = '<';
  3988. #else /* not IBMR2AIX */
  3989.   keymap.hfkey[0].hf_page = '<';
  3990. #endif /* not IBMR2AIX */
  3991.   keymap.hfkey[0].hf_char = 8;
  3992.   keymap.hfkey[1].hf_kpos = 15;
  3993.   keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
  3994. #ifdef IBMR2AIX
  3995.   keymap.hfkey[1].hf_keyidh = '<';
  3996. #else /* not IBMR2AIX */
  3997.   keymap.hfkey[1].hf_page = '<';
  3998. #endif /* not IBMR2AIX */
  3999.   keymap.hfkey[1].hf_char = 8;
  4000.   hftctl (0, HFSKBD, &buf);
  4001. }
  4002.  
  4003. #endif /* AIX */
  4004.  
  4005. /**
  4006.  **  (sjk)++ set default unixmode and default stack.
  4007.  **/
  4008.  
  4009. #ifdef atarist
  4010. static char    __default_unixmode[16] = "/.,rCLAHdb\0\0\0\0\0";
  4011. extern char *_default_unixmode = (char *)__default_unixmode;
  4012. extern long _initial_stack = 512 * 1024L;
  4013.  
  4014.  
  4015. DEFUN ("st-set-unixmode", Fst_set_unixmode, Sst_set_unixmode, 1, 1,
  4016.   "sNew UNIXMODE setting: ",
  4017.   "Set the UNIXMODE defaults.\n\
  4018. This string describes how filenames are changed by emacs to match\n\
  4019. the standards of the TOS filesystem. The supported flags are:\n\
  4020.  b\tUse binary mode when opening file streams (no CR, only LF).\n\
  4021.  c\tAssume case is already significant to the operating system.\n\
  4022.  d\tAllow special file names like /dev/console.\n\
  4023.  rX\tFilenames starting with '/' are rooted from drive X:.\n\
  4024.  u\tAssume the OS allows unlimited length file names already.\n\
  4025.  x\tRecognize executable files by their magic number.\n\
  4026.  /\tTranslate the directory seperator '/' in filenames to '\\'.\n\
  4027.  .x\tTranslate extra dots and other non-file chars to 'x'.\n\
  4028.  L\tSwitch on processing of symbolic links.\n\
  4029. The following flags are meaningful only if 'L' is present.\n\
  4030.  A\tSwitch on automatic link generation for changed filenames.\n\
  4031.  H\tHide the special file \".dir\" from directory searches.")
  4032.   (mode)
  4033.     Lisp_Object mode;
  4034. {
  4035.   CHECK_STRING (mode, 0);
  4036.   _set_unixmode (XSTRING (mode)->data);
  4037.   /* if you want more control, eg setting the environment var UNIXMODE too, */
  4038.   /* or validating the argument, you can do this in lisp code */
  4039.   return Qt;
  4040. }
  4041.  
  4042. void
  4043. init_system()
  4044. { _binmode(1);              /* binary mode is default in fopen calls */
  4045. }
  4046.  
  4047. void
  4048. syms_of_system ()
  4049. {
  4050.   defsubr (&Sst_set_unixmode);
  4051. }
  4052. #endif /* atarist */
  4053.